Skip to content

Commit

Permalink
Refactoring some functions, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pahatz committed Dec 8, 2023
1 parent 39f1a4b commit e860df6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ You can login to download the configuration file needed for some of the the tool
```bash
./sda-cli login <login_target>
```
where `login_target` is the URL can be the login endpoint for Big Picture (https://login.bp.nbis.se/), Federated EGA (https://login.fega.nbis.se/) or Genomic Data Infrastructure (https://login.gdi.nbis.se/)
where `login_target` is the URL to the `sda-auth` service from the [sensitive-data-archive](https://github.com/neicnordic/sensitive-data-archive/) project.

This will open a link for the user where they can go and log in.
After the login is complete, a configuration file will be created in the tool's directory with the name of `.sda-cli-session`
Expand Down
35 changes: 20 additions & 15 deletions encrypt/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,40 @@ func Encrypt(args []string) error {
return err
}

var sesKey string
if len(publicKeyFileList) == 0 {
// check for public key in .sda-cli-session file from login
sesKey, err = helpers.GetPublicKeyFromSession()
if err != nil {
log.Println("could not read key from previous login,", err)
}
}
// key from session file found
if len(publicKeyFileList) == 0 && sesKey != "" {
publicKeyFileList = append(publicKeyFileList, sesKey)
var pubKey string

if publicKeyFileList != nil && *target != "" {
return errors.New("only one of -key or -target can be used")
}
if len(publicKeyFileList) == 0 && sesKey == "" && *target != "" {

if publicKeyFileList == nil && *target != "" {
// fetch info endpoint values
info, err := login.GetAuthInfo(*target)
if err != nil {
return err
}
// create pub file
pubFile, err := helpers.CreatePubFile(info.PublicKey, "crypt4gh_key.pub")
pubKeyFile, err := helpers.CreatePubFile(info.PublicKey, "crypt4gh_key.pub")
if err != nil {
return err
}
log.Println("fetching public key")
// no key provided, no key in session file, target provided
publicKeyFileList = append(publicKeyFileList, pubFile)
publicKeyFileList = append(publicKeyFileList, pubKeyFile)
}
// no key provided, no key in session file, no target provided
if len(publicKeyFileList) == 0 && sesKey == "" && *target == "" {
if publicKeyFileList == nil && *target == "" {
// check for public key in .sda-cli-session file from login
pubKey, err = helpers.GetPublicKeyFromSession()
if err != nil {
log.Println("could not read key from previous login,", err)
}
// key from session file found
if len(publicKeyFileList) == 0 && pubKey != "" {
publicKeyFileList = append(publicKeyFileList, pubKey)
}
}
if publicKeyFileList == nil {
return errors.New("no public key could be obtained")
}

Expand Down
12 changes: 4 additions & 8 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,9 @@ func GetPublicKeyFromSession() (string, error) {
return "", errors.New("configuration file (.sda-cli-session) not found")
}

if FileExists(".sda-cli-session") {
file, err := os.Open(".sda-cli-session")
if err != nil {
fmt.Println("could not read file:", file)
}
file, err := os.Open(".sda-cli-session")
if err != nil {
fmt.Println("could not read file:", file)
}

// Load the configuration file
Expand All @@ -327,13 +325,12 @@ func GetPublicKeyFromSession() (string, error) {

// Create public key file
func CreatePubFile(publicKey string, filename string) (string, error) {

// Create a fixed-size array to hold the public key data
var publicKeyData [32]byte
b := []byte(publicKey)
copy(publicKeyData[:], b)

// Open or create a file named "key-from-oidc.pub.pem" in write-only mode with file permissions 0600
// Open or create a file in write-only mode with file permissions 0600
pubFile, err := os.OpenFile(filepath.Clean(filename), os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return "", fmt.Errorf("failed to open or create the public key file: %w", err)
Expand All @@ -344,7 +341,6 @@ func CreatePubFile(publicKey string, filename string) (string, error) {
log.Errorf("Error closing file: %s\n", cerr)
}
}()

// Write the publicKeyData array to the "key-from-oidc.pub.pem" file in Crypt4GHX25519 public key format
err = keys.WriteCrypt4GHX25519PublicKey(pubFile, publicKeyData)
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ encrypt = False
assert.EqualError(suite.T(), err, "public key not found in the configuration")
}

func (suite *HelperTests) TestGetPublicKey() {
func (suite *HelperTests) TestGetPublicKeyFromSession() {

var confFile = `
access_token = someToken
Expand Down Expand Up @@ -389,3 +389,17 @@ func (suite *HelperTests) TestInvalidCharacters() {
assert.Equal(suite.T(), fmt.Sprintf("filepath %v contains disallowed characters: %+v", testfilepath, badchar), err.Error())
}
}

func (suite *HelperTests) TestCreatePubFile() {
var pubKeyContent = `339eb2a458fec5e23aa8b57cfcb35f10e7389025816e44d4234f814ed2aeed3f`
var expectedPubKey = `-----BEGIN CRYPT4GH PUBLIC KEY-----
MzM5ZWIyYTQ1OGZlYzVlMjNhYThiNTdjZmNiMzVmMTA=
-----END CRYPT4GH PUBLIC KEY-----
`
_, err := CreatePubFile(pubKeyContent, "test_public_file.pub.pem")
assert.NoError(suite.T(), err)

pubFile, _ := os.ReadFile("test_public_file.pub.pem")
s := string(pubFile)
assert.Equal(suite.T(), expectedPubKey, s)
}
3 changes: 3 additions & 0 deletions helpers/testcreate.pub.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN CRYPT4GH PUBLIC KEY-----
MzM5ZWIyYTQ1OGZlYzVlMjNhYThiNTdjZmNiMzVmMTA=
-----END CRYPT4GH PUBLIC KEY-----
4 changes: 2 additions & 2 deletions login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var ArgHelp = `
[login-target]
The login target can be one of the following:
https://login.bp.nbis.se/
https://login.test.fega.nbis.se/
https://login.fega.nbis.se/
https://login.gdi.nbis.se/`

// Args is a flagset that needs to be exported so that it can be written to the
Expand Down Expand Up @@ -168,7 +168,7 @@ func NewLogin(args []string) error {
}
err = deviceLogin.Login()
if err != nil {
return fmt.Errorf("Login failed")
return err
}
fmt.Printf("Logged in as %v\n", deviceLogin.UserInfo.Name)

Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func ParseArgs() (string, []string) {

if Help(subcommand) == nil {
os.Exit(0)
} else {
os.Exit(1)
}
os.Exit(1)

}

// The "list" command can have no arguments since it can use the
Expand Down

0 comments on commit e860df6

Please sign in to comment.