Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce env. variable NOTARY_AUTH for setting username & password #1246

Merged
merged 1 commit into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions cmd/notary/tuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
Expand Down Expand Up @@ -785,6 +786,30 @@ func (ps passwordStore) Basic(u *url.URL) (string, string) {
return "", ""
}

auth := os.Getenv("NOTARY_AUTH")
if auth != "" {
dec, err := base64.StdEncoding.DecodeString(auth)
if err != nil {
logrus.Error("Could not base64-decode authentication string")
return "", ""
}
plain := string(dec)

i := strings.Index(plain, ":")
if i == 0 {
logrus.Error("Authentication string with zero-legnth username")
return "", ""
} else if i > -1 {
username := plain[:i]
password := plain[i+1:]
password = strings.TrimSpace(password)
return username, password
}

logrus.Error("Malformatted authentication string; format must be <username>:<password>")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm debating whether it makes sense for this to be a fatal error here. This is only used in the CLI so it seems like it would be desirable to exit ASAP if we've made the decision the inputs are bad. Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be fine by me. I followed the existing pattern where we don't exit with fatal error on timeout from keyboard input, either. Maybe that should then be changed as well in either a preceding patch or one on top of this here...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases would not be able to test false inputs, though. Running into failures would exit the test case...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm yeah, definitely prefer having the tests work properly. We'll leave this as is 👍

return "", ""
}

stdin := bufio.NewReader(os.Stdin)
input := make(chan string, 1)
fmt.Fprintf(os.Stdout, "Enter username: ")
Expand Down
28 changes: 28 additions & 0 deletions cmd/notary/tuf_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/base64"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -308,3 +309,30 @@ func TestPasswordStore(t *testing.T) {
require.Equal(t, "", ps.RefreshToken(myurl, "someService"))
}
}

func TestPasswordStoreWithEnvvar(t *testing.T) {
myurl, err := url.Parse("https://docker.io")
require.NoError(t, err)

ps := passwordStore{}

creds := base64.StdEncoding.EncodeToString([]byte("me:mypassword"))
os.Setenv("NOTARY_AUTH", creds)

username, passwd := ps.Basic(myurl)
require.Equal(t, "me", username)
require.Equal(t, "mypassword", passwd)

creds = base64.StdEncoding.EncodeToString([]byte(":mypassword"))
os.Setenv("NOTARY_AUTH", creds)

username, passwd = ps.Basic(myurl)
require.Equal(t, "", username)
require.Equal(t, "", passwd)

os.Setenv("NOTARY_AUTH", "not-base64-encoded")

username, passwd = ps.Basic(myurl)
require.Equal(t, "", username)
require.Equal(t, "", passwd)
}