Skip to content

Commit 4655f10

Browse files
iofqneolynx
authored andcommitted
feat: Allow Jfrog Artifactory authentication to be configured via env var
1 parent 3333a64 commit 4655f10

2 files changed

Lines changed: 82 additions & 27 deletions

File tree

jfrog/public.go

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,45 @@ var (
3030
_ aptly.PublishedStorage = (*PublishedStorage)(nil)
3131
)
3232

33-
// NewPublishedStorageRaw creates jfrog PublishedStorage from raw connection specs
34-
func NewPublishedStorageRaw(
35-
repository, url, user, password, apiKey, accessToken, prefix string,
36-
plusWorkaround, debug bool,
37-
) (*PublishedStorage, error) {
38-
33+
func createPublishedStorageConfig(url, user, password, apiKey, accessToken string) (config.Config, error) {
3934
artDetails := auth.NewArtifactoryDetails()
4035
artDetails.SetUrl(url)
36+
37+
if user == "" {
38+
user = os.Getenv("JFROG_USERNAME");
39+
}
40+
if password == "" {
41+
password = os.Getenv("JFROG_PASSWORD");
42+
}
43+
if apiKey == "" {
44+
apiKey = os.Getenv("JFROG_APIKEY");
45+
}
46+
if accessToken == "" {
47+
accessToken = os.Getenv("JFROG_ACCESSTOKEN");
48+
}
49+
4150
if user != "" && password != "" {
42-
artDetails.SetUser(user)
43-
artDetails.SetPassword(password)
51+
artDetails.SetUser(user)
52+
artDetails.SetPassword(password)
4453
} else if apiKey != "" {
45-
artDetails.SetApiKey(apiKey)
54+
artDetails.SetApiKey(apiKey)
4655
} else if accessToken != "" {
4756
artDetails.SetAccessToken(accessToken)
48-
}
57+
}
4958

50-
serviceConfig, err := config.NewConfigBuilder().
59+
return config.NewConfigBuilder().
5160
SetServiceDetails(artDetails).
5261
SetDryRun(false).
5362
Build()
54-
63+
}
64+
65+
// NewPublishedStorageRaw creates jfrog PublishedStorage from raw connection specs
66+
func NewPublishedStorageRaw(
67+
repository, url, user, password, apiKey, accessToken, prefix string,
68+
plusWorkaround, debug bool,
69+
) (*PublishedStorage, error) {
70+
71+
serviceConfig, err := createPublishedStorageConfig(url, user, password, apiKey, accessToken)
5572
if err != nil {
5673
return nil, errors.Wrap(err, "error building jfrog client config")
5774
}
@@ -91,7 +108,7 @@ func (storage *PublishedStorage) PutFile(path string, sourceFilename string) err
91108
if storage.plusWorkaround {
92109
targetPath = strings.Replace(targetPath, "+", "%2B", -1)
93110
}
94-
111+
95112
params := services.NewUploadParams()
96113
params.Pattern = sourceFilename
97114
params.Target = targetPath
@@ -106,7 +123,7 @@ func (storage *PublishedStorage) Remove(path string) error {
106123
if storage.plusWorkaround {
107124
targetPath = strings.Replace(targetPath, "+", "%2B", -1)
108125
}
109-
126+
110127
deleteParams := services.NewDeleteParams()
111128
deleteParams.SetPattern(targetPath)
112129

@@ -175,7 +192,7 @@ func (storage *PublishedStorage) Filelist(prefix string) ([]string, error) {
175192
return nil, err
176193
}
177194
defer reader.Close()
178-
195+
179196
var paths []string
180197

181198
for element := new(utils.ResultItem); reader.NextRecord(element) == nil; element = new(utils.ResultItem) {
@@ -186,19 +203,19 @@ func (storage *PublishedStorage) Filelist(prefix string) ([]string, error) {
186203
}
187204
paths = append(paths, relPath)
188205
}
189-
206+
190207
return paths, nil
191208
}
192209

193210
func (storage *PublishedStorage) RenameFile(oldName, newName string) error {
194211
oldTarget := filepath.Join(storage.repository, storage.prefix, oldName)
195212
newTarget := filepath.Join(storage.repository, storage.prefix, newName)
196-
213+
197214
if storage.plusWorkaround {
198215
oldTarget = strings.Replace(oldTarget, "+", "%2B", -1)
199216
newTarget = strings.Replace(newTarget, "+", "%2B", -1)
200217
}
201-
218+
202219
params := services.NewMoveCopyParams()
203220
params.Pattern = oldTarget
204221
params.Target = newTarget
@@ -211,17 +228,17 @@ func (storage *PublishedStorage) RenameFile(oldName, newName string) error {
211228
func (storage *PublishedStorage) SymLink(src string, dst string) error {
212229
oldTarget := filepath.Join(storage.repository, storage.prefix, src)
213230
newTarget := filepath.Join(storage.repository, storage.prefix, dst)
214-
231+
215232
if storage.plusWorkaround {
216233
oldTarget = strings.Replace(oldTarget, "+", "%2B", -1)
217234
newTarget = strings.Replace(newTarget, "+", "%2B", -1)
218235
}
219-
236+
220237
params := services.NewMoveCopyParams()
221238
params.Pattern = oldTarget
222239
params.Target = newTarget
223240
params.Flat = true
224-
241+
225242
props := utils.NewProperties()
226243
props.AddProperty("SymLink", src)
227244
params.SetTargetProps(props)
@@ -239,7 +256,7 @@ func (storage *PublishedStorage) FileExists(path string) (bool, error) {
239256
if storage.plusWorkaround {
240257
targetPath = strings.Replace(targetPath, "+", "%2B", -1)
241258
}
242-
259+
243260
params := services.NewSearchParams()
244261
params.Pattern = targetPath
245262

@@ -248,7 +265,7 @@ func (storage *PublishedStorage) FileExists(path string) (bool, error) {
248265
return false, err
249266
}
250267
defer reader.Close()
251-
268+
252269
length, err := reader.Length()
253270
isEmpty := length == 0
254271
return !isEmpty, err
@@ -259,17 +276,17 @@ func (storage *PublishedStorage) ReadLink(path string) (string, error) {
259276
if storage.plusWorkaround {
260277
targetPath = strings.Replace(targetPath, "+", "%2B", -1)
261278
}
262-
279+
263280
props, err := storage.manager.GetItemProps(targetPath)
264281
if err != nil {
265282
return "", nil
266283
}
267-
284+
268285
for k, v := range props.Properties {
269286
if k == "SymLink" && len(v) > 0 {
270287
return v[0], nil
271288
}
272289
}
273-
290+
274291
return "", nil
275292
}

jfrog/public_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import (
1818
. "gopkg.in/check.v1"
1919
)
2020

21-
func Test(t *testing.T) { TestingT(t) }
21+
func Test(t *testing.T) {
22+
t.Setenv("JFROG_USERNAME", "userfromenv")
23+
TestingT(t)
24+
}
2225

2326
type fakeJFrogManager struct {
2427
artifactory.EmptyArtifactoryServicesManager
@@ -425,6 +428,41 @@ func (s *PublishedStorageSuite) TestReadLinkPlusWorkaround(c *C) {
425428
c.Assert(s.manager.itemPropsErr, IsNil)
426429
}
427430

431+
func (s *PublishedStorageSuite) TestCreatePublishedStorageConfig(c *C) {
432+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
433+
w.WriteHeader(http.StatusOK)
434+
}))
435+
defer server.Close()
436+
437+
withUserPassword, err := createPublishedStorageConfig(server.URL, "user", "password", "", "")
438+
c.Assert(err, IsNil)
439+
440+
withUserPasswordDetails := withUserPassword.GetServiceDetails()
441+
c.Assert(withUserPasswordDetails, NotNil)
442+
c.Assert(withUserPasswordDetails.GetUser(), Equals, "user")
443+
444+
withAPIKey, err := createPublishedStorageConfig(server.URL, "", "", "api-123", "")
445+
c.Assert(err, IsNil)
446+
447+
withAPIKeyDetails := withAPIKey.GetServiceDetails()
448+
c.Assert(withAPIKeyDetails, NotNil)
449+
c.Assert(withAPIKeyDetails.GetApiKey(), Equals, "api-123")
450+
451+
withAccessToken, err := createPublishedStorageConfig(server.URL, "", "", "", "token")
452+
c.Assert(err, IsNil)
453+
454+
withAccessTokenDetails := withAccessToken.GetServiceDetails()
455+
c.Assert(withAccessTokenDetails, NotNil)
456+
c.Assert(withAccessTokenDetails.GetAccessToken(), Equals, "token")
457+
458+
withUserPasswordFromEnv, err := createPublishedStorageConfig(server.URL, "", "password", "", "")
459+
c.Assert(err, IsNil)
460+
461+
withUserPasswordFromEnvDetails := withUserPasswordFromEnv.GetServiceDetails()
462+
c.Assert(withUserPasswordFromEnvDetails, NotNil)
463+
c.Assert(withUserPasswordFromEnvDetails.GetUser(), Equals, "userfromenv")
464+
}
465+
428466
func (s *PublishedStorageSuite) TestNewPublishedStorageRaw(c *C) {
429467
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
430468
w.WriteHeader(http.StatusOK)

0 commit comments

Comments
 (0)