helper.go 1.9 KB

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