From d89e3adf2c2389e159a30f94d42b9a530ff0d7ad Mon Sep 17 00:00:00 2001 From: Sijie Date: Mon, 4 Mar 2024 19:10:50 -0800 Subject: [PATCH 01/25] run on mongo7 without update --- .github/workflows/test.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d3cc9ba..bcac2cd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,9 +28,13 @@ jobs: - go: '1.12' mongo: 'mongodb-linux-x86_64-3.6.20' minio: '2019-05-23T00-29-34Z' + wired_tiger: 'true' + - go: '1.12' + mongo: 'mongodb-linux-x86_64-ubuntu2204-7.0.4' + minio: '2019-05-23T00-29-34Z' wired_tiger: 'false' - go: '1.12' - mongo: 'mongodb-linux-x86_64-3.6.20' + mongo: 'mongodb-linux-x86_64-ubuntu2204-7.0.4' minio: '2019-05-23T00-29-34Z' wired_tiger: 'true' steps: From 6a7139510ab6254b265da89710105b8f97ae07ba Mon Sep 17 00:00:00 2001 From: Sijie Date: Fri, 8 Mar 2024 17:26:57 -0800 Subject: [PATCH 02/25] add function getMongoDBVer and fix --nojournal arg --- test/mongocontroller/controller.go | 37 ++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/test/mongocontroller/controller.go b/test/mongocontroller/controller.go index 96869f7..ff26bc9 100644 --- a/test/mongocontroller/controller.go +++ b/test/mongocontroller/controller.go @@ -6,6 +6,7 @@ import ( "os/exec" "path/filepath" "strconv" + "strings" "time" "go.mongodb.org/mongo-driver/mongo" @@ -28,7 +29,7 @@ type Params struct { UseWiredTiger bool } -// Controller is a Minio controller. +// Controller is a Mongo controller. type Controller struct { port int tempDir string @@ -53,14 +54,25 @@ func New(p Params) (*Controller, error) { if err != nil { return nil, err } + cmdargs := []string{ "--port", strconv.Itoa(port), "--dbpath", ddir, - "--nojournal", + } + + // check mongodb version + ver, err := getMongoDBVer(p.ExecutablePath) + if err != nil { + return nil, err + } + + if ver.LessThan(*semver.New("6.1")) { + cmdargs = append(cmdargs, "--nojournal") } if p.UseWiredTiger { cmdargs = append(cmdargs, "--storageEngine", "wiredTiger") } + cmd := exec.Command(p.ExecutablePath, cmdargs...) cmd.Stdout = outfile cmd.Stderr = outfile @@ -80,18 +92,8 @@ func New(p Params) (*Controller, error) { if err != nil { return nil, err } - res := client.Database("foo").RunCommand(nil, map[string]int{"buildinfo": 1}) - if res.Err() != nil { - return nil, res.Err() - } - var doc map[string]interface{} - err = res.Decode(&doc) - if err != nil { - return nil, err - } // wired tiger will also not include index names for 3.0, but we're not going to test // that so screw it - ver := semver.New(doc["version"].(string)) return &Controller{port, tdir, cmd, ver.LessThan(*semver.New("3.2.1000"))}, nil } @@ -121,3 +123,14 @@ func (c *Controller) Destroy(deleteTempDir bool) error { } return nil } + +func getMongoDBVer(ExecutablePath string) (*semver.Version, error) { + cmd := exec.Command(ExecutablePath, "--version") + stdout, err := cmd.Output() + if err != nil { + return nil, err + } + rep := strings.Replace(string(stdout), "\n", " ", -1) + ver := strings.Split(rep, " ")[2][1:] + return semver.New(ver), err +} From b1fc0008ed13cf2517418a8ab8da5bd2e9d8e3a2 Mon Sep 17 00:00:00 2001 From: Sijie Date: Fri, 8 Mar 2024 17:35:45 -0800 Subject: [PATCH 03/25] add more ref for nojournal option --- test/mongocontroller/controller.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/mongocontroller/controller.go b/test/mongocontroller/controller.go index ff26bc9..08e78f8 100644 --- a/test/mongocontroller/controller.go +++ b/test/mongocontroller/controller.go @@ -66,7 +66,11 @@ func New(p Params) (*Controller, error) { return nil, err } - if ver.LessThan(*semver.New("6.1")) { + // Starting in MongoDB 6.1, journaling is always enabled. + // As a result, MongoDB removes the storage.journal.enabled option and + // the corresponding --journal and --nojournal command-line options. + // https://www.mongodb.com/docs/manual/release-notes/6.1/#changes-to-journaling + if ver.LessThan(*semver.New("6.1.0")) { cmdargs = append(cmdargs, "--nojournal") } if p.UseWiredTiger { From d0afe815106f8f0fec4eb477b2537f3b79c1bbef Mon Sep 17 00:00:00 2001 From: Sijie Date: Fri, 8 Mar 2024 23:33:36 -0800 Subject: [PATCH 04/25] remove namespace check --- nodestore/mongostore_test.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/nodestore/mongostore_test.go b/nodestore/mongostore_test.go index 2302a91..617f924 100644 --- a/nodestore/mongostore_test.go +++ b/nodestore/mongostore_test.go @@ -773,7 +773,7 @@ func (t *TestSuite) TestConfigIndexes() { "_id_": false, "schema_1": true, } - t.checkIndexes("config", testDB+".config", expected) + t.checkIndexes("config", expected) } func (t *TestSuite) TestUserIndexes() { @@ -782,7 +782,7 @@ func (t *TestSuite) TestUserIndexes() { "user_1": true, "id_1": true, } - t.checkIndexes("users", testDB+".users", expected) + t.checkIndexes("users", expected) } func (t *TestSuite) TestNodeIndexes() { @@ -790,12 +790,11 @@ func (t *TestSuite) TestNodeIndexes() { "_id_": false, "id_1": true, } - t.checkIndexes("nodes", testDB+".nodes", expected) + t.checkIndexes("nodes", expected) } func (t *TestSuite) checkIndexes( collection string, - expectedNamespace string, expectedIndexes map[string]bool) { _, err := NewMongoNodeStore(t.client.Database(testDB)) if err != nil { @@ -815,7 +814,6 @@ func (t *TestSuite) checkIndexes( t.Fail(err.Error()) } m := elem.Map() - t.Equal(expectedNamespace, m["ns"], "incorrect name space") if un, ok := m["unique"]; ok { names[m["name"].(string)] = un.(bool) } else { From d143ada251f4715d915ed73c0b55d25204c708bc Mon Sep 17 00:00:00 2001 From: Sijie Date: Fri, 8 Mar 2024 23:49:36 -0800 Subject: [PATCH 05/25] fix error --- nodestore/mongostore_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nodestore/mongostore_test.go b/nodestore/mongostore_test.go index 617f924..d42bad6 100644 --- a/nodestore/mongostore_test.go +++ b/nodestore/mongostore_test.go @@ -105,7 +105,8 @@ func (t *TestSuite) TestConstructFailAddConfigIndex() { t.Contains(err.Error(), "E11000 duplicate key error", "incorrect error") t.Contains(err.Error(), testDB+".config", "incorrect error") t.Contains(err.Error(), "schema_1", "incorrect error") - t.Contains(err.Error(), "dup key: { : \"schema\" }", "incorrect error") + t.Contains(err.Error(), "dup key: { ", "incorrect error") + t.Contains(err.Error(), ": \"schema\" }", "incorrect error") } func (t *TestSuite) TestConstructFailTwoConfigDocs() { From d1286a3d11e3a19adffac72f47b986d3ea44cc74 Mon Sep 17 00:00:00 2001 From: Sijie Date: Mon, 11 Mar 2024 20:40:48 -0700 Subject: [PATCH 06/25] add auth shadow jar --- test/kbaseauthcontroller/authjars.go | 127 ++++++++++++++------------- 1 file changed, 64 insertions(+), 63 deletions(-) diff --git a/test/kbaseauthcontroller/authjars.go b/test/kbaseauthcontroller/authjars.go index af96fd9..8234db2 100644 --- a/test/kbaseauthcontroller/authjars.go +++ b/test/kbaseauthcontroller/authjars.go @@ -10,67 +10,68 @@ const ( // required for the auth server. // might want to make the paths non-unix specific... yagni var jars = []string{ - "kbase/auth2/kbase-auth2-0.2.4.jar", - "kbase/auth2/kbase-auth2test-0.2.4.jar", - //lib - "apache_commons/commons-codec-1.8.jar", - "apache_commons/commons-validator-1.5.1.jar", - "google/guava-18.0.jar", - "ini4j/ini4j-0.5.2.jar", - "jcommander/jcommander-1.48.jar", - "mongo/mongo-java-driver-3.3.0.jar", - "mustache/compiler-0.9.3.jar", - "nulab-inc/zxcvbn-1.2.2.jar", - //logging - "kbase/common/kbase-common-0.0.22.jar", - "jna/jna-3.4.0.jar", - "logback/logback-core-1.1.2.jar", - "logback/logback-classic-1.1.2.jar", - "slf4j/slf4j-api-1.7.25.jar", - "syslog4j/syslog4j-0.9.46.jar", - //yauaa - "yauaa/yauaa-1.3.jar", - "apache_commons/commons-lang3-3.5.jar", - "apache_commons/commons-collections4-4.1.jar", - "apache_commons/commons-logging-1.2.jar", - "kohsuke/args4j-2.33.jar", - "snakeyaml/snakeyaml-1.18.jar", - //jackson - "jackson/jackson-annotations-2.5.4.jar", - "jackson/jackson-core-2.5.4.jar", - "jackson/jackson-databind-2.5.4.jar", - "jackson/jackson-jaxrs-base-2.5.4.jar", - "jackson/jackson-jaxrs-json-provider-2.5.4.jar", - "jackson/jackson-module-jaxb-annotations-2.5.4.jar", - //jersey - "jersey/entity-filtering/jersey-entity-filtering-2.23.2.jar", - "jersey/entity-filtering/jersey-media-json-jackson-2.23.2.jar", - "jersey/mvc/jersey-mvc-2.23.2.jar", - "jersey/mvc/jersey-mvc-mustache-2.23.2.jar", - "jersey/jersey-client-2.23.2.jar", - "jersey/jersey-common-2.23.2.jar", - "jersey/jersey-container-servlet-2.23.2.jar", - "jersey/jersey-container-servlet-core-2.23.2.jar", - "jersey/jersey-guava-2.23.2.jar", - "jersey/jersey-media-jaxb-2.23.2.jar", - "jersey/jersey-server-2.23.2.jar", - //jerseydeps - "annotation/javax.annotation-api-1.2.jar", - "asm/asm-debug-all-5.0.4.jar", - "inject/javax.inject-2.5.0-b05.jar", - "javassist/javassist-3.20.0-GA.jar", - "jaxb/jaxb-api-2.2.7.jar", - "jaxrs/javax.ws.rs-api-2.0.1.jar", - "osgi/org.osgi.core-4.2.0.jar", - "persistence/persistence-api-1.0.jar", - "servlet/javax.servlet-api-3.0.1.jar", - "validationapi/validation-api-1.1.0.Final.jar", - //jerseydep_hk2 - "hk2/aopalliance-repackaged-2.5.0-b05.jar", - "hk2/hk2-api-2.5.0-b05.jar", - "hk2/hk2-locator-2.5.0-b05.jar", - "hk2/hk2-utils-2.5.0-b05.jar", - "hk2/osgi-resource-locator-1.0.1.jar", - //test - "jetty/jetty-all-9.3.11.v20160721-uber.jar", + "kbase/auth2/kbase-auth2-test-shadow-all-0.7.0.jar", + // "kbase/auth2/kbase-auth2-0.2.4.jar", + // "kbase/auth2/kbase-auth2test-0.2.4.jar", + // //lib + // "apache_commons/commons-codec-1.8.jar", + // "apache_commons/commons-validator-1.5.1.jar", + // "google/guava-18.0.jar", + // "ini4j/ini4j-0.5.2.jar", + // "jcommander/jcommander-1.48.jar", + // "mongo/mongo-java-driver-3.3.0.jar", + // "mustache/compiler-0.9.3.jar", + // "nulab-inc/zxcvbn-1.2.2.jar", + // //logging + // "kbase/common/kbase-common-0.0.22.jar", + // "jna/jna-3.4.0.jar", + // "logback/logback-core-1.1.2.jar", + // "logback/logback-classic-1.1.2.jar", + // "slf4j/slf4j-api-1.7.25.jar", + // "syslog4j/syslog4j-0.9.46.jar", + // //yauaa + // "yauaa/yauaa-1.3.jar", + // "apache_commons/commons-lang3-3.5.jar", + // "apache_commons/commons-collections4-4.1.jar", + // "apache_commons/commons-logging-1.2.jar", + // "kohsuke/args4j-2.33.jar", + // "snakeyaml/snakeyaml-1.18.jar", + // //jackson + // "jackson/jackson-annotations-2.5.4.jar", + // "jackson/jackson-core-2.5.4.jar", + // "jackson/jackson-databind-2.5.4.jar", + // "jackson/jackson-jaxrs-base-2.5.4.jar", + // "jackson/jackson-jaxrs-json-provider-2.5.4.jar", + // "jackson/jackson-module-jaxb-annotations-2.5.4.jar", + // //jersey + // "jersey/entity-filtering/jersey-entity-filtering-2.23.2.jar", + // "jersey/entity-filtering/jersey-media-json-jackson-2.23.2.jar", + // "jersey/mvc/jersey-mvc-2.23.2.jar", + // "jersey/mvc/jersey-mvc-mustache-2.23.2.jar", + // "jersey/jersey-client-2.23.2.jar", + // "jersey/jersey-common-2.23.2.jar", + // "jersey/jersey-container-servlet-2.23.2.jar", + // "jersey/jersey-container-servlet-core-2.23.2.jar", + // "jersey/jersey-guava-2.23.2.jar", + // "jersey/jersey-media-jaxb-2.23.2.jar", + // "jersey/jersey-server-2.23.2.jar", + // //jerseydeps + // "annotation/javax.annotation-api-1.2.jar", + // "asm/asm-debug-all-5.0.4.jar", + // "inject/javax.inject-2.5.0-b05.jar", + // "javassist/javassist-3.20.0-GA.jar", + // "jaxb/jaxb-api-2.2.7.jar", + // "jaxrs/javax.ws.rs-api-2.0.1.jar", + // "osgi/org.osgi.core-4.2.0.jar", + // "persistence/persistence-api-1.0.jar", + // "servlet/javax.servlet-api-3.0.1.jar", + // "validationapi/validation-api-1.1.0.Final.jar", + // //jerseydep_hk2 + // "hk2/aopalliance-repackaged-2.5.0-b05.jar", + // "hk2/hk2-api-2.5.0-b05.jar", + // "hk2/hk2-locator-2.5.0-b05.jar", + // "hk2/hk2-utils-2.5.0-b05.jar", + // "hk2/osgi-resource-locator-1.0.1.jar", + // //test + // "jetty/jetty-all-9.3.11.v20160721-uber.jar", } From 95e7f5c41fd186ab3585d10179c821647af4e184 Mon Sep 17 00:00:00 2001 From: Sijie Date: Mon, 11 Mar 2024 21:14:19 -0700 Subject: [PATCH 07/25] finish & clean up --- test/kbaseauthcontroller/authjars.go | 76 ++------------------------ test/kbaseauthcontroller/controller.go | 12 ++-- 2 files changed, 9 insertions(+), 79 deletions(-) diff --git a/test/kbaseauthcontroller/authjars.go b/test/kbaseauthcontroller/authjars.go index 8234db2..455684e 100644 --- a/test/kbaseauthcontroller/authjars.go +++ b/test/kbaseauthcontroller/authjars.go @@ -1,77 +1,11 @@ package kbaseauthcontroller -// this file simply lists the jars that are required to run the KBase auth server in test mode. +// this file simply lists the template and the jar in the KBase jars repo (https://github.com/kbase/jars) +// that are required to run the KBase auth server in test mode. const ( // authTemplates is the zip file containing templates for the server authTemplates = "kbase/auth2/kbase-auth2templates-0.2.4.zip" -) -// jars contains the list of jars in the KBase jars repo (https://github.com/kbase/jars) -// required for the auth server. -// might want to make the paths non-unix specific... yagni -var jars = []string{ - "kbase/auth2/kbase-auth2-test-shadow-all-0.7.0.jar", - // "kbase/auth2/kbase-auth2-0.2.4.jar", - // "kbase/auth2/kbase-auth2test-0.2.4.jar", - // //lib - // "apache_commons/commons-codec-1.8.jar", - // "apache_commons/commons-validator-1.5.1.jar", - // "google/guava-18.0.jar", - // "ini4j/ini4j-0.5.2.jar", - // "jcommander/jcommander-1.48.jar", - // "mongo/mongo-java-driver-3.3.0.jar", - // "mustache/compiler-0.9.3.jar", - // "nulab-inc/zxcvbn-1.2.2.jar", - // //logging - // "kbase/common/kbase-common-0.0.22.jar", - // "jna/jna-3.4.0.jar", - // "logback/logback-core-1.1.2.jar", - // "logback/logback-classic-1.1.2.jar", - // "slf4j/slf4j-api-1.7.25.jar", - // "syslog4j/syslog4j-0.9.46.jar", - // //yauaa - // "yauaa/yauaa-1.3.jar", - // "apache_commons/commons-lang3-3.5.jar", - // "apache_commons/commons-collections4-4.1.jar", - // "apache_commons/commons-logging-1.2.jar", - // "kohsuke/args4j-2.33.jar", - // "snakeyaml/snakeyaml-1.18.jar", - // //jackson - // "jackson/jackson-annotations-2.5.4.jar", - // "jackson/jackson-core-2.5.4.jar", - // "jackson/jackson-databind-2.5.4.jar", - // "jackson/jackson-jaxrs-base-2.5.4.jar", - // "jackson/jackson-jaxrs-json-provider-2.5.4.jar", - // "jackson/jackson-module-jaxb-annotations-2.5.4.jar", - // //jersey - // "jersey/entity-filtering/jersey-entity-filtering-2.23.2.jar", - // "jersey/entity-filtering/jersey-media-json-jackson-2.23.2.jar", - // "jersey/mvc/jersey-mvc-2.23.2.jar", - // "jersey/mvc/jersey-mvc-mustache-2.23.2.jar", - // "jersey/jersey-client-2.23.2.jar", - // "jersey/jersey-common-2.23.2.jar", - // "jersey/jersey-container-servlet-2.23.2.jar", - // "jersey/jersey-container-servlet-core-2.23.2.jar", - // "jersey/jersey-guava-2.23.2.jar", - // "jersey/jersey-media-jaxb-2.23.2.jar", - // "jersey/jersey-server-2.23.2.jar", - // //jerseydeps - // "annotation/javax.annotation-api-1.2.jar", - // "asm/asm-debug-all-5.0.4.jar", - // "inject/javax.inject-2.5.0-b05.jar", - // "javassist/javassist-3.20.0-GA.jar", - // "jaxb/jaxb-api-2.2.7.jar", - // "jaxrs/javax.ws.rs-api-2.0.1.jar", - // "osgi/org.osgi.core-4.2.0.jar", - // "persistence/persistence-api-1.0.jar", - // "servlet/javax.servlet-api-3.0.1.jar", - // "validationapi/validation-api-1.1.0.Final.jar", - // //jerseydep_hk2 - // "hk2/aopalliance-repackaged-2.5.0-b05.jar", - // "hk2/hk2-api-2.5.0-b05.jar", - // "hk2/hk2-locator-2.5.0-b05.jar", - // "hk2/hk2-utils-2.5.0-b05.jar", - // "hk2/osgi-resource-locator-1.0.1.jar", - // //test - // "jetty/jetty-all-9.3.11.v20160721-uber.jar", -} + // auth2ShadowAllJar is the jar required for the server + authShadowAllJar = "kbase/auth2/kbase-auth2-test-shadow-all-0.7.0.jar" +) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index ccc9795..2686158 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -124,15 +124,11 @@ func getClassPath(jarsDir string) (string, error) { if err != nil { return "", err } - cp := []string(nil) - for _, j := range jars { // global variable, yech - jpath := path.Join(jarsDir, j) - if _, err := os.Stat(jpath); os.IsNotExist(err) { - return "", fmt.Errorf("Jar %v does not exist", jpath) - } - cp = append(cp, jpath) + jpath := path.Join(jarsDir, authShadowAllJar) + if _, err := os.Stat(jpath); os.IsNotExist(err) { + return "", fmt.Errorf("Jar %v does not exist", jpath) } - return strings.Join(cp, ":"), nil + return jpath, nil } func installTemplates(jarsDir string, templateDir string) error { From d2b48b7e2ab70b44a9b1981e0b003871d311268b Mon Sep 17 00:00:00 2001 From: Sijie Date: Wed, 13 Mar 2024 12:46:00 -0700 Subject: [PATCH 08/25] update test.yml, var name, and mongo connection testing --- .github/workflows/test.yml | 8 -------- test/kbaseauthcontroller/authjars.go | 11 ----------- test/kbaseauthcontroller/controller.go | 5 +++++ test/mongocontroller/controller.go | 10 ++++++++-- 4 files changed, 13 insertions(+), 21 deletions(-) delete mode 100644 test/kbaseauthcontroller/authjars.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bcac2cd..829b409 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,14 +25,6 @@ jobs: mongo: 'mongodb-linux-x86_64-3.6.12' minio: '2019-05-23T00-29-34Z' wired_tiger: 'false' - - go: '1.12' - mongo: 'mongodb-linux-x86_64-3.6.20' - minio: '2019-05-23T00-29-34Z' - wired_tiger: 'true' - - go: '1.12' - mongo: 'mongodb-linux-x86_64-ubuntu2204-7.0.4' - minio: '2019-05-23T00-29-34Z' - wired_tiger: 'false' - go: '1.12' mongo: 'mongodb-linux-x86_64-ubuntu2204-7.0.4' minio: '2019-05-23T00-29-34Z' diff --git a/test/kbaseauthcontroller/authjars.go b/test/kbaseauthcontroller/authjars.go deleted file mode 100644 index 455684e..0000000 --- a/test/kbaseauthcontroller/authjars.go +++ /dev/null @@ -1,11 +0,0 @@ -package kbaseauthcontroller - -// this file simply lists the template and the jar in the KBase jars repo (https://github.com/kbase/jars) -// that are required to run the KBase auth server in test mode. -const ( - // authTemplates is the zip file containing templates for the server - authTemplates = "kbase/auth2/kbase-auth2templates-0.2.4.zip" - - // auth2ShadowAllJar is the jar required for the server - authShadowAllJar = "kbase/auth2/kbase-auth2-test-shadow-all-0.7.0.jar" -) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index 2686158..bdd07be 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -22,8 +22,13 @@ import ( "github.com/phayes/freeport" ) +// The following are required to run the KBase auth server in test mode. const ( serverClass = "us.kbase.test.auth2.StandaloneAuthServer" + // authTemplates is the zip file containing templates for the server + authTemplates = "kbase/auth2/kbase-auth2templates-0.2.4.zip" + // auth2ShadowAllJar is the jar required for the server + authShadowAllJar = "kbase/auth2/kbase-auth2-test-shadow-all-0.7.0.jar" ) // Params are Parameters for creating a KBase Auth2 service (https://github.com/kbase/auth2) diff --git a/test/mongocontroller/controller.go b/test/mongocontroller/controller.go index 08e78f8..4156793 100644 --- a/test/mongocontroller/controller.go +++ b/test/mongocontroller/controller.go @@ -96,6 +96,12 @@ func New(p Params) (*Controller, error) { if err != nil { return nil, err } + + // test mongo connection + res := client.Database("foo").RunCommand(context.TODO(), map[string]int{"buildinfo": 1}) + if res.Err() != nil { + return nil, res.Err() + } // wired tiger will also not include index names for 3.0, but we're not going to test // that so screw it return &Controller{port, tdir, cmd, ver.LessThan(*semver.New("3.2.1000"))}, nil @@ -128,8 +134,8 @@ func (c *Controller) Destroy(deleteTempDir bool) error { return nil } -func getMongoDBVer(ExecutablePath string) (*semver.Version, error) { - cmd := exec.Command(ExecutablePath, "--version") +func getMongoDBVer(executablePath string) (*semver.Version, error) { + cmd := exec.Command(executablePath, "--version") stdout, err := cmd.Output() if err != nil { return nil, err From 2ecb09a069f0605098f8db3beaa598663eb88b38 Mon Sep 17 00:00:00 2001 From: Sijie Date: Wed, 13 Mar 2024 13:38:23 -0700 Subject: [PATCH 09/25] check test connection && compare mongo version --- test/mongocontroller/controller.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/mongocontroller/controller.go b/test/mongocontroller/controller.go index 4156793..18f09f7 100644 --- a/test/mongocontroller/controller.go +++ b/test/mongocontroller/controller.go @@ -2,6 +2,7 @@ package mongocontroller import ( "context" + "fmt" "os" "os/exec" "path/filepath" @@ -102,6 +103,15 @@ func New(p Params) (*Controller, error) { if res.Err() != nil { return nil, res.Err() } + var doc map[string]interface{} + err = res.Decode(&doc) + if err != nil { + return nil, err + } + if ver.String() != doc["version"].(string) { + return nil, fmt.Errorf("the two mongo versions should be the same: %s != %s", + ver.String(), doc["version"].(string)) + } // wired tiger will also not include index names for 3.0, but we're not going to test // that so screw it return &Controller{port, tdir, cmd, ver.LessThan(*semver.New("3.2.1000"))}, nil From 71babb3999b38b07847f6aaa7fb724604166d584 Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 13:33:32 -0700 Subject: [PATCH 10/25] update all the way --- .github/workflows/test.yml | 12 ++-- auth/kbase_provider_test.go | 2 +- service/integration_test.go | 2 +- test.cfg.example | 5 +- test/kbaseauthcontroller/controller.go | 81 +++++++++++++++++--------- test/testhelpers/config.go | 10 ++-- 6 files changed, 69 insertions(+), 43 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 829b409..9a806ff 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,6 +14,9 @@ on: - master - develop +env: + AUTH2_JAR_NAME: kbase-auth2-test-shadow-all-0.7.0.jar + jobs: workspace_deluxe_tests: runs-on: ubuntu-latest @@ -46,10 +49,9 @@ jobs: # move to parent dir to install binaries etc cd .. - # set up jars - # TODO switch to the auth shadow jar - git clone https://github.com/kbase/jars - export JARSDIR=`pwd`/jars/lib/jars/ + # set up auth2 jar + wget -q https://github.com/kbase/jars/raw/master/lib/jars/kbase/auth2/$AUTH2_JAR_NAME + export AUTH2JAR=`pwd`/$AUTH2_JAR_NAME # set up mongo wget -q http://fastdl.mongodb.org/linux/${{matrix.mongo}}.tgz @@ -68,7 +70,7 @@ jobs: sed -i "s#^test.mongo.exe.*#test.mongo.exe=$MONGOD#" test.cfg sed -i "s#^test.minio.exe.*#test.minio.exe=$MINIO#" test.cfg sed -i "s#^test.mongo.wired_tiger.*#test.mongo.wired_tiger=${{matrix.wired_tiger}}#" test.cfg - sed -i "s#^test.jars.dir.*#test.jars.dir=$JARSDIR#" test.cfg + sed -i "s#^test.auth2jar.*#test.auth2jar=$AUTH2JAR#" test.cfg cat test.cfg - name: Run tests diff --git a/auth/kbase_provider_test.go b/auth/kbase_provider_test.go index d520618..7340088 100644 --- a/auth/kbase_provider_test.go +++ b/auth/kbase_provider_test.go @@ -53,7 +53,7 @@ func (t *TestSuite) SetupSuite() { t.mongo = mongoctl auth, err := kbaseauthcontroller.New(kbaseauthcontroller.Params{ - JarsDir: tcfg.JarsDir, + Auth2Jar: tcfg.Auth2JarPath, MongoHost: "localhost:" + strconv.Itoa(mongoctl.GetPort()), MongoDatabase: "test_kb_auth_provider_authdb", RootTempDir: tcfg.TempDir, diff --git a/service/integration_test.go b/service/integration_test.go index 18034b3..ae4a07b 100644 --- a/service/integration_test.go +++ b/service/integration_test.go @@ -191,7 +191,7 @@ func (t *TestSuite) addTestRole(username string, role string) { func (t *TestSuite) setupAuth(cfg *testhelpers.TestConfig, ) (*kbaseauthcontroller.Controller, url.URL) { auth, err := kbaseauthcontroller.New(kbaseauthcontroller.Params{ - JarsDir: cfg.JarsDir, + Auth2Jar: cfg.Auth2JarPath, MongoHost: "localhost:" + strconv.Itoa(t.mongo.GetPort()), MongoDatabase: "test_kb_auth_provider_authdb", RootTempDir: cfg.TempDir, diff --git a/test.cfg.example b/test.cfg.example index 6e8a729..4825897 100644 --- a/test.cfg.example +++ b/test.cfg.example @@ -10,9 +10,8 @@ test.mongo.exe=mongod # no. test.mongo.wired_tiger=false -# The path to the jars dir inside the jars repo (https://github.com/kbase/jars), e.g. -# [path to jars repo]/lib/jars -test.jars.dir = +# The path to the auth2 jar +test.auth2jar= # Where to store temporary files generated during the test. test.temp.dir=temp_test_dir diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index bdd07be..c20fd6e 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -1,7 +1,6 @@ package kbaseauthcontroller import ( - "archive/zip" "bytes" "encoding/json" "errors" @@ -22,20 +21,15 @@ import ( "github.com/phayes/freeport" ) -// The following are required to run the KBase auth server in test mode. const ( serverClass = "us.kbase.test.auth2.StandaloneAuthServer" - // authTemplates is the zip file containing templates for the server - authTemplates = "kbase/auth2/kbase-auth2templates-0.2.4.zip" - // auth2ShadowAllJar is the jar required for the server - authShadowAllJar = "kbase/auth2/kbase-auth2-test-shadow-all-0.7.0.jar" ) // Params are Parameters for creating a KBase Auth2 service (https://github.com/kbase/auth2) // controller. type Params struct { - // JarsDir is the path to the /lib/jars directory of the - JarsDir string + // Auth2Jar is the path to the kbase auth2 jar. + Auth2Jar string // MongoHost is the mongo host. MongoHost string // MongoDatabase is the database to use for auth data. @@ -53,7 +47,7 @@ type Controller struct { // New creates a new controller. func New(p Params) (*Controller, error) { - classPath, err := getClassPath(p.JarsDir) + classPath, err := getClassPath(p.Auth2Jar) if err != nil { return nil, err } @@ -63,7 +57,7 @@ func New(p Params) (*Controller, error) { if err != nil { return nil, err } - err = installTemplates(p.JarsDir, templateDir) + err = installTemplates(p.Auth2Jar, templateDir) if err != nil { return nil, err } @@ -124,46 +118,77 @@ func waitForStartup(port string) error { return startupErr } -func getClassPath(jarsDir string) (string, error) { - jarsDir, err := filepath.Abs(jarsDir) +func getClassPath(auth2Jar string) (string, error) { + jpath, err := filepath.Abs(auth2Jar) if err != nil { return "", err } - jpath := path.Join(jarsDir, authShadowAllJar) if _, err := os.Stat(jpath); os.IsNotExist(err) { - return "", fmt.Errorf("Jar %v does not exist", jpath) + return "", fmt.Errorf("jar %v does not exist", jpath) } return jpath, nil } -func installTemplates(jarsDir string, templateDir string) error { - templateZip := path.Join(jarsDir, authTemplates) - arch, err := zip.OpenReader(templateZip) // global variable, yech +func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { + dirPath := filepath.Dir(classPath) + outfile, err := os.Create(filepath.Join(dirPath, "output.txt")) + if err != nil { + return "", err + } + + cmdargs := []string{classPath, "-d", dirPath} + cmd := exec.Command("unzip", cmdargs...) + cmd.Stdout = outfile + cmd.Stderr = outfile + err = cmd.Start() + if err != nil { + return "", err + } + + tpath := filepath.Join(dirPath, "kbase_auth2_templates") + if _, err := os.Stat(tpath); os.IsNotExist(err) { + return "", fmt.Errorf("the template folder %v does not exist", tpath) + } + return tpath, err +} + +func installTemplates(classPath string, templateDir string) error { + tpath, err := pullTemplatesOutofAuth2Jar(classPath) if err != nil { return err } - for _, f := range arch.File { - name := f.FileHeader.Name + files, err := os.ReadDir(tpath) + if err != nil { + return err + } + for _, f := range files { + name := f.Name() if !strings.HasSuffix(name, "/") { // not a directory name = path.Clean(name) if path.IsAbs(name) || strings.HasPrefix(name, "..") { - return fmt.Errorf("Zip file %v contains files outside the zip directory - "+ - "this is a sign of a malicious zip file", templateZip) + return fmt.Errorf("template folder %v contains files outside the directory - "+ + "this is a sign of a malicious template folder", tpath) } - target, err := filepath.Abs(path.Join(templateDir, name)) + dst, err := filepath.Abs(path.Join(templateDir, name)) if err != nil { return err } - os.MkdirAll(path.Dir(target), 0600) - r, err := f.Open() + os.MkdirAll(path.Dir(dst), 0600) + + src := filepath.Join(tpath, name) + source, err := os.Open(src) + if err != nil { + return err + } + defer source.Close() + + destination, err := os.Create(dst) if err != nil { return err } - f, err := os.Create(target) + defer destination.Close() - io.Copy(f, r) - r.Close() - f.Close() + io.Copy(destination, source) } } return nil diff --git a/test/testhelpers/config.go b/test/testhelpers/config.go index 42b045f..b233e65 100644 --- a/test/testhelpers/config.go +++ b/test/testhelpers/config.go @@ -21,8 +21,8 @@ const ( TestMongoExe = "test.mongo.exe" // TestUseWiredTiger denotes that the MongoDB WiredTiger storage engine should be used. TestUseWiredTiger = "test.mongo.wired_tiger" - // TestJarsDir is the key in the config file for the path to the KBase jars directory. - TestJarsDir = "test.jars.dir" + // TestAuth2Jar is the key in the config file for the path to the KBase auth2 jar. + TestAuth2Jar = "test.auth2jar" // TestTempDir is the key in the config file for the temporary directory. TestTempDir = "test.temp.dir" // TestDeleteTempDir is the key in the config file for whether the temporary directory @@ -36,7 +36,7 @@ type TestConfig struct { MinioExePath string MongoExePath string UseWiredTiger bool - JarsDir string + Auth2JarPath string TempDir string DeleteTempDir bool } @@ -71,7 +71,7 @@ func GetConfig() (*TestConfig, error) { if err != nil { return nil, err } - jarsdir, err := getValue(sec, TestJarsDir, configfile, true) + auth2Jar, err := getValue(sec, TestAuth2Jar, configfile, true) if err != nil { return nil, err } @@ -88,7 +88,7 @@ func GetConfig() (*TestConfig, error) { MinioExePath: minio, MongoExePath: mongo, UseWiredTiger: wiredTiger == "true", - JarsDir: jarsdir, + Auth2JarPath: auth2Jar, TempDir: tempDir, DeleteTempDir: del != "false", }, From 6dc56fcba45d0d7b4ea4886ade9f8813098222fd Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 13:38:43 -0700 Subject: [PATCH 11/25] fix spacing issue --- test/kbaseauthcontroller/controller.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index c20fd6e..3ab98e2 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -157,8 +157,8 @@ func installTemplates(classPath string, templateDir string) error { if err != nil { return err } - files, err := os.ReadDir(tpath) - if err != nil { + files, err := os.ReadDir(tpath) + if err != nil { return err } for _, f := range files { From f859c671bdb3b5b08fe8c09af307708327a5a5e3 Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 13:45:26 -0700 Subject: [PATCH 12/25] switch to ioutil --- test/kbaseauthcontroller/controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index 3ab98e2..af1f800 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -157,7 +157,7 @@ func installTemplates(classPath string, templateDir string) error { if err != nil { return err } - files, err := os.ReadDir(tpath) + files, err := ioutil.ReadDir(tpath) if err != nil { return err } From f1c744518ac821ce216a8319ca9632bb299d1640 Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 14:14:56 -0700 Subject: [PATCH 13/25] use jar xf to unzip --- test/kbaseauthcontroller/controller.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index af1f800..80d8ca7 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -136,8 +136,8 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { return "", err } - cmdargs := []string{classPath, "-d", dirPath} - cmd := exec.Command("unzip", cmdargs...) + cmdargs := []string{"xf", classPath} + cmd := exec.Command("jar", cmdargs...) cmd.Stdout = outfile cmd.Stderr = outfile err = cmd.Start() From 408b570d2c3ccf8890d3164e21dfe72a11f79a6b Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 14:20:58 -0700 Subject: [PATCH 14/25] fix path bug --- test/kbaseauthcontroller/controller.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index 80d8ca7..a3379d3 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -57,7 +57,7 @@ func New(p Params) (*Controller, error) { if err != nil { return nil, err } - err = installTemplates(p.Auth2Jar, templateDir) + err = installTemplates(classPath, templateDir) if err != nil { return nil, err } @@ -136,8 +136,8 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { return "", err } - cmdargs := []string{"xf", classPath} - cmd := exec.Command("jar", cmdargs...) + cmdargs := []string{classPath, "-d", dirPath} + cmd := exec.Command("unzip", cmdargs...) cmd.Stdout = outfile cmd.Stderr = outfile err = cmd.Start() From 33d9ee77a36727bd839925148d1256fafcaa738e Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 14:28:19 -0700 Subject: [PATCH 15/25] display all files --- test/kbaseauthcontroller/controller.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index a3379d3..dd46bad 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -131,6 +131,7 @@ func getClassPath(auth2Jar string) (string, error) { func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { dirPath := filepath.Dir(classPath) + fmt.Printf("the parent dir of auth2 is located at %v", dirPath) outfile, err := os.Create(filepath.Join(dirPath, "output.txt")) if err != nil { return "", err @@ -145,6 +146,11 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { return "", err } + files, _ := ioutil.ReadDir(dirPath) + for _, f := range files { + fmt.Print(f.Name()) + } + tpath := filepath.Join(dirPath, "kbase_auth2_templates") if _, err := os.Stat(tpath); os.IsNotExist(err) { return "", fmt.Errorf("the template folder %v does not exist", tpath) @@ -153,6 +159,7 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { } func installTemplates(classPath string, templateDir string) error { + fmt.Printf("the auth2 is located at %v", classPath) tpath, err := pullTemplatesOutofAuth2Jar(classPath) if err != nil { return err From 6369af91534854d866533f75ca2e1bef11259766 Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 14:32:34 -0700 Subject: [PATCH 16/25] display with println --- test/kbaseauthcontroller/controller.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index dd46bad..d4180f5 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -131,7 +131,9 @@ func getClassPath(auth2Jar string) (string, error) { func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { dirPath := filepath.Dir(classPath) + fmt.Println("------------------------------") fmt.Printf("the parent dir of auth2 is located at %v", dirPath) + fmt.Println("------------------------------") outfile, err := os.Create(filepath.Join(dirPath, "output.txt")) if err != nil { return "", err @@ -148,7 +150,7 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { files, _ := ioutil.ReadDir(dirPath) for _, f := range files { - fmt.Print(f.Name()) + fmt.Println(f.Name()) } tpath := filepath.Join(dirPath, "kbase_auth2_templates") @@ -159,7 +161,9 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { } func installTemplates(classPath string, templateDir string) error { + fmt.Println("------------------------------") fmt.Printf("the auth2 is located at %v", classPath) + fmt.Println("------------------------------") tpath, err := pullTemplatesOutofAuth2Jar(classPath) if err != nil { return err From 2c8f9d173350613ab930af670d73a341389f86b5 Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 14:41:54 -0700 Subject: [PATCH 17/25] sleep 1 sec --- test/kbaseauthcontroller/controller.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index d4180f5..c053450 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -153,6 +153,8 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { fmt.Println(f.Name()) } + time.Sleep(1 * time.Second) + tpath := filepath.Join(dirPath, "kbase_auth2_templates") if _, err := os.Stat(tpath); os.IsNotExist(err) { return "", fmt.Errorf("the template folder %v does not exist", tpath) From 19ebf3b1107995a9e56a6c8d62ad09af4ed6959b Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 15:28:44 -0700 Subject: [PATCH 18/25] check kbase_auth2_templates existence --- test/kbaseauthcontroller/controller.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index c053450..0dcd679 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -23,6 +23,9 @@ import ( const ( serverClass = "us.kbase.test.auth2.StandaloneAuthServer" + + // The max number of attempts to check if templates have been pulled out of the shadow jar + attempts = 10 ) // Params are Parameters for creating a KBase Auth2 service (https://github.com/kbase/auth2) @@ -148,15 +151,17 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { return "", err } - files, _ := ioutil.ReadDir(dirPath) - for _, f := range files { - fmt.Println(f.Name()) - } - - time.Sleep(1 * time.Second) - + i := 0 tpath := filepath.Join(dirPath, "kbase_auth2_templates") - if _, err := os.Stat(tpath); os.IsNotExist(err) { + for i < attempts { + if _, err := os.Stat(tpath); os.IsNotExist(err) { + time.Sleep(1 * time.Second) + i += 1 + continue + } + break + } + if i == attempts { return "", fmt.Errorf("the template folder %v does not exist", tpath) } return tpath, err From 04daa79e3a7206e4151d51ddb3c56b8665521b0d Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 15:34:22 -0700 Subject: [PATCH 19/25] clean up && finish --- test/kbaseauthcontroller/controller.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index 0dcd679..2003e06 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -134,9 +134,6 @@ func getClassPath(auth2Jar string) (string, error) { func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { dirPath := filepath.Dir(classPath) - fmt.Println("------------------------------") - fmt.Printf("the parent dir of auth2 is located at %v", dirPath) - fmt.Println("------------------------------") outfile, err := os.Create(filepath.Join(dirPath, "output.txt")) if err != nil { return "", err @@ -151,8 +148,9 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { return "", err } - i := 0 tpath := filepath.Join(dirPath, "kbase_auth2_templates") + + i := 0 for i < attempts { if _, err := os.Stat(tpath); os.IsNotExist(err) { time.Sleep(1 * time.Second) @@ -168,9 +166,6 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { } func installTemplates(classPath string, templateDir string) error { - fmt.Println("------------------------------") - fmt.Printf("the auth2 is located at %v", classPath) - fmt.Println("------------------------------") tpath, err := pullTemplatesOutofAuth2Jar(classPath) if err != nil { return err From 8576aab0b7facf660a854bc050046ac18c25e358 Mon Sep 17 00:00:00 2001 From: Sijie Date: Thu, 14 Mar 2024 15:54:58 -0700 Subject: [PATCH 20/25] move pullTemplatesOutofAuth2Jar into New --- test/kbaseauthcontroller/controller.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index 2003e06..18a795e 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -54,13 +54,17 @@ func New(p Params) (*Controller, error) { if err != nil { return nil, err } + tpath, err := pullTemplatesOutofAuth2Jar(classPath) + if err != nil { + return nil, err + } tdir := filepath.Join(p.RootTempDir, "AuthController-"+uuid.New().String()) templateDir := filepath.Join(tdir, "templates") err = os.MkdirAll(templateDir, 0700) if err != nil { return nil, err } - err = installTemplates(classPath, templateDir) + err = installTemplates(tpath, templateDir) if err != nil { return nil, err } @@ -165,11 +169,7 @@ func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { return tpath, err } -func installTemplates(classPath string, templateDir string) error { - tpath, err := pullTemplatesOutofAuth2Jar(classPath) - if err != nil { - return err - } +func installTemplates(tpath string, templateDir string) error { files, err := ioutil.ReadDir(tpath) if err != nil { return err From 8503abaf4ad4214ba995de81692013bdfe1c241d Mon Sep 17 00:00:00 2001 From: Sijie Date: Fri, 15 Mar 2024 10:38:33 -0700 Subject: [PATCH 21/25] address all comments --- test.cfg.example | 2 +- test/kbaseauthcontroller/controller.go | 69 +++++++------------------- test/testhelpers/config.go | 2 +- 3 files changed, 20 insertions(+), 53 deletions(-) diff --git a/test.cfg.example b/test.cfg.example index 4825897..b58f209 100644 --- a/test.cfg.example +++ b/test.cfg.example @@ -10,7 +10,7 @@ test.mongo.exe=mongod # no. test.mongo.wired_tiger=false -# The path to the auth2 jar +# The path to the kbase auth2 service shadow test jar test.auth2jar= # Where to store temporary files generated during the test. diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index 18a795e..e96c140 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -1,6 +1,7 @@ package kbaseauthcontroller import ( + "archive/zip" "bytes" "encoding/json" "errors" @@ -23,9 +24,6 @@ import ( const ( serverClass = "us.kbase.test.auth2.StandaloneAuthServer" - - // The max number of attempts to check if templates have been pulled out of the shadow jar - attempts = 10 ) // Params are Parameters for creating a KBase Auth2 service (https://github.com/kbase/auth2) @@ -50,11 +48,7 @@ type Controller struct { // New creates a new controller. func New(p Params) (*Controller, error) { - classPath, err := getClassPath(p.Auth2Jar) - if err != nil { - return nil, err - } - tpath, err := pullTemplatesOutofAuth2Jar(classPath) + authJarPath, err := checkAuthJarExists(p.Auth2Jar) if err != nil { return nil, err } @@ -64,7 +58,7 @@ func New(p Params) (*Controller, error) { if err != nil { return nil, err } - err = installTemplates(tpath, templateDir) + err = installTemplates(authJarPath, templateDir) if err != nil { return nil, err } @@ -78,7 +72,7 @@ func New(p Params) (*Controller, error) { } strport := strconv.Itoa(port) cmdargs := []string{ - "-classpath", classPath, + "-classpath", authJarPath, "-DAUTH2_TEST_MONGOHOST=" + p.MongoHost, "-DAUTH2_TEST_MONGODB=" + p.MongoDatabase, "-DAUTH2_TEST_TEMPLATE_DIR=" + templateDir, @@ -125,7 +119,7 @@ func waitForStartup(port string) error { return startupErr } -func getClassPath(auth2Jar string) (string, error) { +func checkAuthJarExists(auth2Jar string) (string, error) { jpath, err := filepath.Abs(auth2Jar) if err != nil { return "", err @@ -136,51 +130,25 @@ func getClassPath(auth2Jar string) (string, error) { return jpath, nil } -func pullTemplatesOutofAuth2Jar(classPath string) (string, error) { - dirPath := filepath.Dir(classPath) - outfile, err := os.Create(filepath.Join(dirPath, "output.txt")) +func installTemplates(authJarPath string, templateDir string) error { + buff, err := ioutil.ReadFile(authJarPath) //read the content of file if err != nil { - return "", err + return err } - cmdargs := []string{classPath, "-d", dirPath} - cmd := exec.Command("unzip", cmdargs...) - cmd.Stdout = outfile - cmd.Stderr = outfile - err = cmd.Start() + r, err := zip.NewReader(bytes.NewReader(buff), int64(len(buff))) if err != nil { - return "", err - } - - tpath := filepath.Join(dirPath, "kbase_auth2_templates") - - i := 0 - for i < attempts { - if _, err := os.Stat(tpath); os.IsNotExist(err) { - time.Sleep(1 * time.Second) - i += 1 - continue - } - break - } - if i == attempts { - return "", fmt.Errorf("the template folder %v does not exist", tpath) + return err } - return tpath, err -} -func installTemplates(tpath string, templateDir string) error { - files, err := ioutil.ReadDir(tpath) - if err != nil { - return err - } - for _, f := range files { - name := f.Name() - if !strings.HasSuffix(name, "/") { // not a directory + for _, f := range r.File { + name := f.Name + // not a directory + if !strings.HasSuffix(name, "/") && strings.HasPrefix(name, "kbase_auth2_templates") { name = path.Clean(name) - if path.IsAbs(name) || strings.HasPrefix(name, "..") { - return fmt.Errorf("template folder %v contains files outside the directory - "+ - "this is a sign of a malicious template folder", tpath) + if path.IsAbs(name) { + return fmt.Errorf("jar file %v contains files outside the directory - "+ + "this is a sign of a malicious jar file", authJarPath) } dst, err := filepath.Abs(path.Join(templateDir, name)) if err != nil { @@ -188,8 +156,7 @@ func installTemplates(tpath string, templateDir string) error { } os.MkdirAll(path.Dir(dst), 0600) - src := filepath.Join(tpath, name) - source, err := os.Open(src) + source, err := f.Open() if err != nil { return err } diff --git a/test/testhelpers/config.go b/test/testhelpers/config.go index b233e65..918ca58 100644 --- a/test/testhelpers/config.go +++ b/test/testhelpers/config.go @@ -21,7 +21,7 @@ const ( TestMongoExe = "test.mongo.exe" // TestUseWiredTiger denotes that the MongoDB WiredTiger storage engine should be used. TestUseWiredTiger = "test.mongo.wired_tiger" - // TestAuth2Jar is the key in the config file for the path to the KBase auth2 jar. + // TestAuth2Jar is the key in the config file for the path to the KBase auth2 shadow test jar. TestAuth2Jar = "test.auth2jar" // TestTempDir is the key in the config file for the temporary directory. TestTempDir = "test.temp.dir" From 651d281ceaf2dd66ec3738cf60324676fd955ab9 Mon Sep 17 00:00:00 2001 From: Sijie Date: Fri, 15 Mar 2024 10:49:58 -0700 Subject: [PATCH 22/25] debug permission denied --- test/kbaseauthcontroller/controller.go | 1 + 1 file changed, 1 insertion(+) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index e96c140..cc51a9a 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -168,6 +168,7 @@ func installTemplates(authJarPath string, templateDir string) error { } defer destination.Close() + fmt.Println("pass this line") io.Copy(destination, source) } } From b30088a2bae9e86b0a0575b64f53b8e1e3f9cae8 Mon Sep 17 00:00:00 2001 From: Sijie Date: Fri, 15 Mar 2024 10:59:36 -0700 Subject: [PATCH 23/25] fix bug --- test/kbaseauthcontroller/controller.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index cc51a9a..d85c429 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -150,7 +150,7 @@ func installTemplates(authJarPath string, templateDir string) error { return fmt.Errorf("jar file %v contains files outside the directory - "+ "this is a sign of a malicious jar file", authJarPath) } - dst, err := filepath.Abs(path.Join(templateDir, name)) + dst, err := filepath.Abs(path.Join(templateDir, filepath.Base(name))) if err != nil { return err } @@ -168,7 +168,6 @@ func installTemplates(authJarPath string, templateDir string) error { } defer destination.Close() - fmt.Println("pass this line") io.Copy(destination, source) } } From a3cc1fe25fe2b977a794fb103aabc8f1f665497b Mon Sep 17 00:00:00 2001 From: Sijie Date: Fri, 15 Mar 2024 11:09:24 -0700 Subject: [PATCH 24/25] replace nil by context.Background() --- test/mongocontroller/controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mongocontroller/controller.go b/test/mongocontroller/controller.go index 18f09f7..42b572e 100644 --- a/test/mongocontroller/controller.go +++ b/test/mongocontroller/controller.go @@ -99,7 +99,7 @@ func New(p Params) (*Controller, error) { } // test mongo connection - res := client.Database("foo").RunCommand(context.TODO(), map[string]int{"buildinfo": 1}) + res := client.Database("foo").RunCommand(context.Background(), map[string]int{"buildinfo": 1}) if res.Err() != nil { return nil, res.Err() } From 9771d7e8c791cb0b94aaef992cb154ac546ebe8f Mon Sep 17 00:00:00 2001 From: Sijie Date: Fri, 15 Mar 2024 13:53:32 -0700 Subject: [PATCH 25/25] fix file check --- test/kbaseauthcontroller/controller.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/test/kbaseauthcontroller/controller.go b/test/kbaseauthcontroller/controller.go index d85c429..99689f5 100644 --- a/test/kbaseauthcontroller/controller.go +++ b/test/kbaseauthcontroller/controller.go @@ -131,22 +131,17 @@ func checkAuthJarExists(auth2Jar string) (string, error) { } func installTemplates(authJarPath string, templateDir string) error { - buff, err := ioutil.ReadFile(authJarPath) //read the content of file + jar, err := zip.OpenReader(authJarPath) if err != nil { return err } - r, err := zip.NewReader(bytes.NewReader(buff), int64(len(buff))) - if err != nil { - return err - } - - for _, f := range r.File { + for _, f := range jar.File { name := f.Name // not a directory if !strings.HasSuffix(name, "/") && strings.HasPrefix(name, "kbase_auth2_templates") { name = path.Clean(name) - if path.IsAbs(name) { + if filepath.Dir(name) != "kbase_auth2_templates" { return fmt.Errorf("jar file %v contains files outside the directory - "+ "this is a sign of a malicious jar file", authJarPath) }