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

[Carry #208] Use the XDG Basedir Spec for the cache #250

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,13 @@ There is no need to use `docker login` or `docker logout`.

## Troubleshooting

Logs from the Amazon ECR Docker Credential Helper are stored in `~/.ecr/log`.
Logs from the Amazon ECR Docker Credential Helper are stored inside you cache
files directory (e.g.: `~/.cache/ecr/log` on Linux, `~/Library/Caches/ecr/log`
on MacOS, or `%LocalAppData%/ecr/log` on Windows). If you upgraded from an
older version of the Amazon ECR Docker Credential Helper, logs may be inside
the old cache directory located at `~/.ecr/log` instead.

For more information about Amazon ECR, see the the
For more information about Amazon ECR, see the
[Amazon Elastic Container Registry User Guide](http://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html).

## Security disclosures
Expand Down
14 changes: 6 additions & 8 deletions ecr-login/cache/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/config"
"github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
)

Expand All @@ -34,13 +33,12 @@ func BuildCredentialsCache(awsSession *session.Session, region string, cacheDir

if cacheDir == "" {
//Get cacheDir from env var "AWS_ECR_CACHE_DIR" or set to default
Copy link
Contributor

Choose a reason for hiding this comment

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

GetCacheDir does more than what this comment explains. How about removing the comment from here?

cacheDir = config.GetCacheDir()
}

cacheDir, err := homedir.Expand(cacheDir)
if err != nil {
logrus.WithError(err).Debug("Could not expand cache path, disabling cache")
return NewNullCredentialsCache()
var err error
cacheDir, err = config.GetCacheDir()
if err != nil {
logrus.WithError(err).Debug("Could not expand cache path, disabling cache")
return NewNullCredentialsCache()
}
}

cacheFilename := "cache.json"
Expand Down
45 changes: 40 additions & 5 deletions ecr-login/config/cache_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,46 @@

package config

import "os"
import (
"os"
"path/filepath"

func GetCacheDir() string {
if cacheDir := os.Getenv("AWS_ECR_CACHE_DIR"); cacheDir != "" {
return cacheDir
"github.com/mitchellh/go-homedir"
)

// GetCacheDir returns the cache directory to use for the credential helper.
// The cache directory is determined in this order:
// 1. The location specified by the AWS_ECR_CACHE_DIR environment variable, if
// set and if expanded validly by homedir.Expand.
// 2. ~/.ecr, if it already exists and is a directory, for
// backwards-compatibility.
// 3. The /ecr directory under the value returned by os.UserCacheDir as the
// default value. os.UserCacheDir returns the OS-specific cache directory.
func GetCacheDir() (string, error) {
var (
cacheDir string
userCacheDir string
err error
)

if cacheDir = os.Getenv("AWS_ECR_CACHE_DIR"); cacheDir != "" {
if cacheDir, err = homedir.Expand(cacheDir); err == nil {
return cacheDir, nil
}
}

cacheDir = "~/.ecr"
if cacheDir, err = homedir.Expand(cacheDir); err == nil {
if info, err := os.Stat(cacheDir); err == nil {
if info.IsDir() {
return cacheDir, nil
}
}
}

userCacheDir, err = os.UserCacheDir()
if err != nil {
return "", err
}
return "~/.ecr"
return filepath.Join(userCacheDir,"/ecr"), nil
}
75 changes: 75 additions & 0 deletions ecr-login/config/cache_dir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package config

import (
"os"
"path/filepath"
"testing"

"github.com/mitchellh/go-homedir"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetCacheDir(t *testing.T) {
// caching is disabled so we can adjust the HOME env var
homedir.DisableCache = true
tempDir := os.TempDir()
defer os.RemoveAll(tempDir)
restore := os.Getenv("HOME")
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", restore)
home, err := homedir.Dir()
require.NoError(t, err, "home must be resolved")
require.Equal(t, tempDir, home, "expect home to be tempDir")
userCache, err := os.UserCacheDir()
require.NoError(t, err, "user cache dir must be resolved")
// Note: tests that adjust environment variables are not thread-safe
t.Run("AWS_ECR_CACHE_DIR valid", func(t *testing.T) {
restore := os.Getenv("AWS_ECR_CACHE_DIR")
os.Setenv("AWS_ECR_CACHE_DIR", "~/testval")
defer os.Setenv("AWS_ECR_CACHE_DIR", restore)

actual, err := GetCacheDir()
assert.NoError(t, err, "should resolve cache dir")
assert.Equal(t, filepath.Join(home, "testval"), actual)
})

t.Run("AWS_ECR_CACHE_DIR invalid", func(t *testing.T) {
restore := os.Getenv("AWS_ECR_CACHE_DIR")
os.Setenv("AWS_ECR_CACHE_DIR", "~testval")
defer os.Setenv("AWS_ECR_CACHE_DIR", restore)

actual, err := GetCacheDir()
assert.NoError(t, err, "should resolve cache dir")
assert.Equal(t, filepath.Join(userCache, "ecr"), actual)
})

t.Run("old exists", func(t *testing.T) {
err := os.Mkdir(filepath.Join(tempDir, ".ecr"), 0755)
require.NoError(t, err)
defer os.RemoveAll(filepath.Join(tempDir, ".ecr"))

actual, err := GetCacheDir()
assert.NoError(t, err, "should resolve cache dir")
assert.Equal(t, filepath.Join(tempDir, ".ecr"), actual)
})

t.Run("default", func(t *testing.T) {
actual, err := GetCacheDir()
assert.NoError(t, err, "should resolve cache dir")
assert.Equal(t, filepath.Join(userCache, "ecr"), actual)
})
}
8 changes: 5 additions & 3 deletions ecr-login/config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"os"
"path/filepath"

"github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
)

Expand All @@ -27,8 +26,11 @@ func SetupLogger() {
}

func logrusConfig() {
logdir, err := homedir.Expand(GetCacheDir() + "/log")
if err != nil {
var logdir string
cacheDir, err := GetCacheDir()
if err == nil {
logdir = filepath.Join(cacheDir, "/log")
} else {
fmt.Fprintf(os.Stderr, "log: failed to find directory: %v", err)
logdir = os.TempDir()
}
Expand Down
11 changes: 5 additions & 6 deletions ecr-login/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/aws/aws-sdk-go v1.24.1 h1:B2NRyTV1/+h+Dg8Bh7vnuvW6QZz/NBL+uzgC2uILDMI=
github.com/aws/aws-sdk-go v1.24.1/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.35.4 h1:GG0sdhmzQSe4/UcF9iuQP9i+58bPRyU4OpujyzMlVjo=
github.com/aws/aws-sdk-go v1.35.4/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -10,18 +8,15 @@ github.com/docker/docker-credential-helpers v0.6.3/go.mod h1:WRaJzqw3CTB9bk10avu
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 h1:tT8iWCYw4uOem71yYA3htfH+LNopJvcqZQshm56G5L4=
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -35,13 +30,17 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 h1:LepdCS8Gf/MVejFIt8lsiexZATdoGVyp5bcyS+rYoUI=
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
28 changes: 28 additions & 0 deletions ecr-login/vendor/github.com/stretchr/testify/require/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading