helper.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package helper
  2. import (
  3. "github.com/docker/docker-credential-helpers/credentials"
  4. "github.com/porter-dev/porter/cli/cmd"
  5. "github.com/porter-dev/porter/cli/cmd/docker"
  6. )
  7. // PorterHelper implements credentials.Helper: it acts as a credentials
  8. // helper for Docker that allows authentication with different registries.
  9. type PorterHelper struct {
  10. Debug bool
  11. ProjectID uint
  12. AuthGetter *docker.AuthGetter
  13. Cache docker.CredentialsCache
  14. }
  15. func NewPorterHelper(debug bool) *PorterHelper {
  16. // get the current project ID
  17. config := cmd.InitAndLoadNewConfig()
  18. cache := docker.NewFileCredentialsCache()
  19. return &PorterHelper{
  20. Debug: debug,
  21. ProjectID: config.Project,
  22. AuthGetter: &docker.AuthGetter{
  23. Client: cmd.GetAPIClient(config),
  24. Cache: cache,
  25. ProjectID: config.Project,
  26. },
  27. Cache: cache,
  28. }
  29. }
  30. // Add appends credentials to the store.
  31. func (p *PorterHelper) Add(cr *credentials.Credentials) error {
  32. // Doesn't seem to be called
  33. return nil
  34. }
  35. // Delete removes credentials from the store.
  36. func (p *PorterHelper) Delete(serverURL string) error {
  37. // Doesn't seem to be called
  38. return nil
  39. }
  40. // Get retrieves credentials from the store.
  41. // It returns username and secret as strings.
  42. func (p *PorterHelper) Get(serverURL string) (user string, secret string, err error) {
  43. return p.AuthGetter.GetCredentials(serverURL)
  44. }
  45. // List returns the stored serverURLs and their associated usernames.
  46. func (p *PorterHelper) List() (map[string]string, error) {
  47. entries := p.Cache.List()
  48. res := make(map[string]string)
  49. for _, entry := range entries {
  50. res[entry.ProxyEndpoint] = entry.AuthorizationToken
  51. }
  52. return res, nil
  53. }