|
| 1 | +package samlcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "time" |
| 11 | + |
| 12 | + homedir "github.com/mitchellh/go-homedir" |
| 13 | + "github.com/pkg/errors" |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + ErrInvalidCachePath = errors.New("Cannot evaluate Cache file path") |
| 19 | + logger = logrus.WithField("pkg", "samlcache") |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + SAMLAssertionValidityTimeout = 5 * time.Minute |
| 24 | + SAMLCacheFilePermissions = 0600 |
| 25 | + SAMLCacheDirPermissions = 0700 |
| 26 | + SAMLCacheDir = "saml2aws" |
| 27 | +) |
| 28 | + |
| 29 | +// SAMLCacheProvider loads aws credentials file |
| 30 | +type SAMLCacheProvider struct { |
| 31 | + Filename string |
| 32 | + Account string |
| 33 | +} |
| 34 | + |
| 35 | +func resolveSymlink(filename string) (string, error) { |
| 36 | + sympath, err := filepath.EvalSymlinks(filename) |
| 37 | + |
| 38 | + // return the un modified filename |
| 39 | + if os.IsNotExist(err) { |
| 40 | + return filename, nil |
| 41 | + } |
| 42 | + if err != nil { |
| 43 | + return "", err |
| 44 | + } |
| 45 | + |
| 46 | + return sympath, nil |
| 47 | +} |
| 48 | + |
| 49 | +func (p *SAMLCacheProvider) IsValid() bool { |
| 50 | + var cache_path string |
| 51 | + var err error |
| 52 | + if p.Filename == "" { |
| 53 | + cache_path, err = locateCacheFile(p.Account) |
| 54 | + if err != nil { |
| 55 | + return false |
| 56 | + } |
| 57 | + } else { |
| 58 | + cache_path = p.Filename |
| 59 | + } |
| 60 | + |
| 61 | + fileInfo, err := os.Stat(cache_path) |
| 62 | + if err != nil { |
| 63 | + return false |
| 64 | + } |
| 65 | + |
| 66 | + return time.Since(fileInfo.ModTime()) < SAMLAssertionValidityTimeout |
| 67 | +} |
| 68 | + |
| 69 | +func locateCacheFile(account string) (string, error) { |
| 70 | + |
| 71 | + var name, filename string |
| 72 | + var err error |
| 73 | + if account == "" { |
| 74 | + filename = "cache" |
| 75 | + } else { |
| 76 | + filename = fmt.Sprintf("cache_%s", account) |
| 77 | + } |
| 78 | + if runtime.GOOS == "windows" { |
| 79 | + name = path.Join(os.Getenv("USERPROFILE"), ".aws", SAMLCacheDir, filename) |
| 80 | + } else { |
| 81 | + name, err = homedir.Expand(path.Join("~", ".aws", SAMLCacheDir, filename)) |
| 82 | + if err != nil { |
| 83 | + return "", ErrInvalidCachePath |
| 84 | + } |
| 85 | + } |
| 86 | + // is the filename a symlink? |
| 87 | + name, err = resolveSymlink(name) |
| 88 | + if err != nil { |
| 89 | + return "", errors.Wrap(err, "unable to resolve symlink") |
| 90 | + } |
| 91 | + |
| 92 | + logger.WithField("name", name).Debug("resolveSymlink") |
| 93 | + |
| 94 | + return name, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (p *SAMLCacheProvider) Read() (string, error) { |
| 98 | + |
| 99 | + var cache_path string |
| 100 | + var err error |
| 101 | + if p.Filename == "" { |
| 102 | + cache_path, err = locateCacheFile(p.Account) |
| 103 | + if err != nil { |
| 104 | + return "", errors.Wrap(err, "Could not retrieve cache file path") |
| 105 | + } |
| 106 | + } else { |
| 107 | + cache_path = p.Filename |
| 108 | + } |
| 109 | + |
| 110 | + content, err := ioutil.ReadFile(cache_path) |
| 111 | + if err != nil { |
| 112 | + return "", errors.Wrap(err, "Could not read the cache file path") |
| 113 | + } |
| 114 | + |
| 115 | + return string(content), nil |
| 116 | +} |
| 117 | + |
| 118 | +func (p *SAMLCacheProvider) Write(samlAssertion string) error { |
| 119 | + |
| 120 | + var cache_path string |
| 121 | + var err error |
| 122 | + if p.Filename == "" { |
| 123 | + cache_path, err = locateCacheFile(p.Account) |
| 124 | + if err != nil { |
| 125 | + return errors.Wrap(err, "Could not retrieve cache file path") |
| 126 | + } |
| 127 | + } else { |
| 128 | + cache_path = p.Filename |
| 129 | + } |
| 130 | + |
| 131 | + // create the directory if it doesn't exist |
| 132 | + err = os.MkdirAll(path.Dir(cache_path), SAMLCacheDirPermissions) |
| 133 | + if err != nil { |
| 134 | + return errors.Wrap(err, "Could not write the cache file directory") |
| 135 | + } |
| 136 | + err = ioutil.WriteFile(cache_path, []byte(samlAssertion), SAMLCacheFilePermissions) |
| 137 | + if err != nil { |
| 138 | + return errors.Wrap(err, "Could not write the cache file path") |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
0 commit comments