Skip to content

Commit

Permalink
update all the way
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangs18 committed Mar 14, 2024
1 parent 2ecb09a commit 71babb3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 43 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion auth/kbase_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion service/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions test.cfg.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 53 additions & 28 deletions test/kbaseauthcontroller/controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kbaseauthcontroller

import (
"archive/zip"
"bytes"
"encoding/json"
"errors"
Expand All @@ -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.
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)

Check failure on line 160 in test/kbaseauthcontroller/controller.go

View workflow job for this annotation

GitHub Actions / workspace_deluxe_tests (1.12, mongodb-linux-x86_64-3.6.12, 2019-05-23T00-29-34Z, false)

undefined: os.ReadDir

Check failure on line 160 in test/kbaseauthcontroller/controller.go

View workflow job for this annotation

GitHub Actions / workspace_deluxe_tests (1.12, mongodb-linux-x86_64-ubuntu2204-7.0.4, 2019-05-23T00-29-34Z, true)

undefined: os.ReadDir
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
Expand Down
10 changes: 5 additions & 5 deletions test/testhelpers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +36,7 @@ type TestConfig struct {
MinioExePath string
MongoExePath string
UseWiredTiger bool
JarsDir string
Auth2JarPath string
TempDir string
DeleteTempDir bool
}
Expand Down Expand Up @@ -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
}
Expand All @@ -88,7 +88,7 @@ func GetConfig() (*TestConfig, error) {
MinioExePath: minio,
MongoExePath: mongo,
UseWiredTiger: wiredTiger == "true",
JarsDir: jarsdir,
Auth2JarPath: auth2Jar,
TempDir: tempDir,
DeleteTempDir: del != "false",
},
Expand Down

0 comments on commit 71babb3

Please sign in to comment.