mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-07-27 13:57:46 +00:00
feat: Allow Jfrog Artifactory authentication to be configured via env var
This commit is contained in:
+33
-16
@@ -30,28 +30,45 @@ var (
|
||||
_ aptly.PublishedStorage = (*PublishedStorage)(nil)
|
||||
)
|
||||
|
||||
func createPublishedStorageConfig(url, user, password, apiKey, accessToken string) (config.Config, error) {
|
||||
artDetails := auth.NewArtifactoryDetails()
|
||||
artDetails.SetUrl(url)
|
||||
|
||||
if user == "" {
|
||||
user = os.Getenv("JFROG_USERNAME");
|
||||
}
|
||||
if password == "" {
|
||||
password = os.Getenv("JFROG_PASSWORD");
|
||||
}
|
||||
if apiKey == "" {
|
||||
apiKey = os.Getenv("JFROG_APIKEY");
|
||||
}
|
||||
if accessToken == "" {
|
||||
accessToken = os.Getenv("JFROG_ACCESSTOKEN");
|
||||
}
|
||||
|
||||
if user != "" && password != "" {
|
||||
artDetails.SetUser(user)
|
||||
artDetails.SetPassword(password)
|
||||
} else if apiKey != "" {
|
||||
artDetails.SetApiKey(apiKey)
|
||||
} else if accessToken != "" {
|
||||
artDetails.SetAccessToken(accessToken)
|
||||
}
|
||||
|
||||
return config.NewConfigBuilder().
|
||||
SetServiceDetails(artDetails).
|
||||
SetDryRun(false).
|
||||
Build()
|
||||
}
|
||||
|
||||
// NewPublishedStorageRaw creates jfrog PublishedStorage from raw connection specs
|
||||
func NewPublishedStorageRaw(
|
||||
repository, url, user, password, apiKey, accessToken, prefix string,
|
||||
plusWorkaround, debug bool,
|
||||
) (*PublishedStorage, error) {
|
||||
|
||||
artDetails := auth.NewArtifactoryDetails()
|
||||
artDetails.SetUrl(url)
|
||||
if user != "" && password != "" {
|
||||
artDetails.SetUser(user)
|
||||
artDetails.SetPassword(password)
|
||||
} else if apiKey != "" {
|
||||
artDetails.SetApiKey(apiKey)
|
||||
} else if accessToken != "" {
|
||||
artDetails.SetAccessToken(accessToken)
|
||||
}
|
||||
|
||||
serviceConfig, err := config.NewConfigBuilder().
|
||||
SetServiceDetails(artDetails).
|
||||
SetDryRun(false).
|
||||
Build()
|
||||
|
||||
serviceConfig, err := createPublishedStorageConfig(url, user, password, apiKey, accessToken)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error building jfrog client config")
|
||||
}
|
||||
|
||||
+39
-1
@@ -18,7 +18,10 @@ import (
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) { TestingT(t) }
|
||||
func Test(t *testing.T) {
|
||||
t.Setenv("JFROG_USERNAME", "userfromenv")
|
||||
TestingT(t)
|
||||
}
|
||||
|
||||
type fakeJFrogManager struct {
|
||||
artifactory.EmptyArtifactoryServicesManager
|
||||
@@ -425,6 +428,41 @@ func (s *PublishedStorageSuite) TestReadLinkPlusWorkaround(c *C) {
|
||||
c.Assert(s.manager.itemPropsErr, IsNil)
|
||||
}
|
||||
|
||||
func (s *PublishedStorageSuite) TestCreatePublishedStorageConfig(c *C) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
withUserPassword, err := createPublishedStorageConfig(server.URL, "user", "password", "", "")
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
withUserPasswordDetails := withUserPassword.GetServiceDetails()
|
||||
c.Assert(withUserPasswordDetails, NotNil)
|
||||
c.Assert(withUserPasswordDetails.GetUser(), Equals, "user")
|
||||
|
||||
withAPIKey, err := createPublishedStorageConfig(server.URL, "", "", "api-123", "")
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
withAPIKeyDetails := withAPIKey.GetServiceDetails()
|
||||
c.Assert(withAPIKeyDetails, NotNil)
|
||||
c.Assert(withAPIKeyDetails.GetApiKey(), Equals, "api-123")
|
||||
|
||||
withAccessToken, err := createPublishedStorageConfig(server.URL, "", "", "", "token")
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
withAccessTokenDetails := withAccessToken.GetServiceDetails()
|
||||
c.Assert(withAccessTokenDetails, NotNil)
|
||||
c.Assert(withAccessTokenDetails.GetAccessToken(), Equals, "token")
|
||||
|
||||
withUserPasswordFromEnv, err := createPublishedStorageConfig(server.URL, "", "password", "", "")
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
withUserPasswordFromEnvDetails := withUserPasswordFromEnv.GetServiceDetails()
|
||||
c.Assert(withUserPasswordFromEnvDetails, NotNil)
|
||||
c.Assert(withUserPasswordFromEnvDetails.GetUser(), Equals, "userfromenv")
|
||||
}
|
||||
|
||||
func (s *PublishedStorageSuite) TestNewPublishedStorageRaw(c *C) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
Reference in New Issue
Block a user