Skip to content

Commit

Permalink
fixup! Add apt credentials parser
Browse files Browse the repository at this point in the history
  • Loading branch information
woky committed Aug 9, 2023
1 parent db609e1 commit 1bcbb91
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
5 changes: 3 additions & 2 deletions internal/archive/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,21 @@ func parseRepoURL(repoURL string) (creds *credentials, query *credentialsQuery,
return
}

const defaultCredsDir = "/etc/apt/auth.conf.d"

var ErrCredentialsNotFound = errors.New("credentials not found")

// findCredentials searches credentials for repoURL in configuration files in
// directory specified by CHISEL_AUTH_DIR environment variable if it's
// non-empty, otherwise /etc/apt/auth.conf.d.
func findCredentials(repoURL string) (*credentials, error) {
credsDir := "/etc/apt/auth.conf.d"
credsDir := defaultCredsDir
if v := os.Getenv("CHISEL_AUTH_DIR"); v != "" {
credsDir = v
}
return findCredentialsInDir(repoURL, credsDir)
}

var FindCredentials = findCredentials
// findCredentialsInDir searches for credentials for repoURL in configuration
// files in credsDir directory. If the directory does not exist, empty
// credentials structure with nil err is returned.
Expand Down
52 changes: 48 additions & 4 deletions internal/archive/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ machine http://example.com/foo login a password
},
}}

func (s *S) TestFindCredentials(c *C) {
func (s *S) TestFindCredentialsInDir(c *C) {
for _, t := range credentialsTests {
s.runFindCredentialsTest(c, &t)
s.runFindCredentialsInDirTest(c, &t)
}
}

func (s *S) runFindCredentialsTest(c *C, t *credentialsTest) {
func (s *S) runFindCredentialsInDirTest(c *C, t *credentialsTest) {
credsDir := c.MkDir()

for filename, data := range t.credsFiles {
Expand All @@ -249,7 +249,7 @@ func (s *S) runFindCredentialsTest(c *C, t *credentialsTest) {
}
}

func (s *S) TestFindCredentialsMissingDir(c *C) {
func (s *S) TestFindCredentialsInDirMissingDir(c *C) {
var creds *archive.Credentials
var err error

Expand Down Expand Up @@ -277,3 +277,47 @@ func (s *S) TestFindCredentialsMissingDir(c *C) {
c.Assert(creds.Username, Equals, "admin")
c.Assert(creds.Password, Equals, "swordfish")
}

func fakeEnv(name, value string) (restore func()) {
origValue, origSet := os.LookupEnv(name)
os.Setenv(name, value)
return func() {
if origSet {
os.Setenv(name, origValue)
} else {
os.Unsetenv(name)
}
}
}

func (s *S) TestFindCredentials(c *C) {
var creds *archive.Credentials
var err error

workDir := c.MkDir()
credsDir := filepath.Join(workDir, "auth.conf.d")

restore := fakeEnv("CHISEL_AUTH_DIR", credsDir)
defer restore()

creds, err = archive.FindCredentials("http://example.com/my/site")
c.Assert(err, ErrorMatches, "^credentials not found$")
c.Assert(creds, IsNil)

err = os.Mkdir(credsDir, 0755)
c.Assert(err, IsNil)

creds, err = archive.FindCredentials("http://example.com/my/site")
c.Assert(err, ErrorMatches, "^credentials not found$")
c.Assert(creds, IsNil)

confFile := filepath.Join(credsDir, "mysite")
err = os.WriteFile(confFile, []byte("machine http://example.com/my login johndoe password 12345"), 0600)
c.Assert(err, IsNil)

creds, err = archive.FindCredentials("http://example.com/my/site")
c.Assert(err, IsNil)
c.Assert(creds, NotNil)
c.Assert(creds.Username, Equals, "johndoe")
c.Assert(creds.Password, Equals, "12345")
}
1 change: 1 addition & 0 deletions internal/archive/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ func FakeDo(do func(req *http.Request) (*http.Response, error)) (restore func())
}

type Credentials = credentials
var FindCredentials = findCredentials
var FindCredentialsInDir = findCredentialsInDir

0 comments on commit 1bcbb91

Please sign in to comment.