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", },