serviceaccount.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package models
  2. import (
  3. "gorm.io/gorm"
  4. )
  5. // Supported auth mechanisms
  6. const (
  7. X509 string = "x509"
  8. Basic = "basic"
  9. Bearer = "bearerToken"
  10. OIDC = "oidc"
  11. GCP = "gcp-sa"
  12. AWS = "aws-sa"
  13. NotAvailable = "n/a"
  14. )
  15. // ServiceAccountCandidate is a service account that requires an action
  16. // from the user to set up.
  17. type ServiceAccountCandidate struct {
  18. gorm.Model
  19. ProjectID uint `json:"project_id"`
  20. Kind string `json:"kind"`
  21. Actions []ServiceAccountAction `json:"actions"`
  22. ClusterName string `json:"cluster_name"`
  23. ClusterEndpoint string `json:"cluster_endpoint"`
  24. AuthMechanism string `json:"auth_mechanism"`
  25. // The best-guess for the AWSClusterID, which is required by aws auth mechanisms
  26. // See https://github.com/kubernetes-sigs/aws-iam-authenticator#what-is-a-cluster-id
  27. AWSClusterIDGuess string `json:"aws_cluster_id_guess"`
  28. // ------------------------------------------------------------------
  29. // All fields below this line are encrypted before storage
  30. // ------------------------------------------------------------------
  31. Kubeconfig []byte `json:"kubeconfig"`
  32. }
  33. // ServiceAccountCandidateExternal represents the ServiceAccountCandidate type that is
  34. // sent over REST
  35. type ServiceAccountCandidateExternal struct {
  36. ID uint `json:"id"`
  37. Actions []ServiceAccountActionExternal `json:"actions"`
  38. ProjectID uint `json:"project_id"`
  39. Kind string `json:"kind"`
  40. ClusterName string `json:"cluster_name"`
  41. ClusterEndpoint string `json:"cluster_endpoint"`
  42. AuthMechanism string `json:"auth_mechanism"`
  43. AWSClusterIDGuess string `json:"aws_cluster_id_guess"`
  44. }
  45. // Externalize generates an external ServiceAccountCandidate to be shared over REST
  46. func (s *ServiceAccountCandidate) Externalize() *ServiceAccountCandidateExternal {
  47. actions := make([]ServiceAccountActionExternal, 0)
  48. for _, action := range s.Actions {
  49. actions = append(actions, *action.Externalize())
  50. }
  51. return &ServiceAccountCandidateExternal{
  52. ID: s.ID,
  53. Actions: actions,
  54. ProjectID: s.ProjectID,
  55. Kind: s.Kind,
  56. ClusterName: s.ClusterName,
  57. ClusterEndpoint: s.ClusterEndpoint,
  58. AuthMechanism: s.AuthMechanism,
  59. AWSClusterIDGuess: s.AWSClusterIDGuess,
  60. }
  61. }
  62. // ServiceAccount type that extends gorm.Model
  63. type ServiceAccount struct {
  64. gorm.Model
  65. ProjectID uint `json:"project_id"`
  66. // Kind can either be "connector" or "provisioner"
  67. Kind string `json:"kind"`
  68. // Clusters is a list of clusters that this ServiceAccount can connect
  69. // to or has provisioned
  70. Clusters []Cluster `json:"clusters"`
  71. // AuthMechanism is the strategy used for either connecting to or provisioning
  72. // the cluster. Supported mechanisms are: basic,x509,bearerToken,oidc,gcp-sa,aws-sa
  73. AuthMechanism string `json:"auth_mechanism"`
  74. // These fields are used by all auth mechanisms
  75. LocationOfOrigin string
  76. Impersonate string `json:"act-as,omitempty"`
  77. ImpersonateGroups string `json:"act-as-groups,omitempty"`
  78. // ------------------------------------------------------------------
  79. // All fields below this line are encrypted before storage
  80. // ------------------------------------------------------------------
  81. // Certificate data is used by x509 auth mechanisms over TLS
  82. ClientCertificateData []byte `json:"client-certificate-data,omitempty"`
  83. ClientKeyData []byte `json:"client-key-data,omitempty"`
  84. // Token is used for bearer-token auth mechanisms
  85. Token string `json:"token,omitempty"`
  86. // Username/Password for basic authentication to a cluster
  87. Username string `json:"username,omitempty"`
  88. Password string `json:"password,omitempty"`
  89. // TokenCache is a cache for bearer tokens with an expiry time
  90. // Used by GCP and AWS mechanisms
  91. TokenCache TokenCache `json:"gcp_token_cache"`
  92. // KeyData for a service account for GCP connectors
  93. GCPKeyData []byte `json:"gcp_key_data"`
  94. // AWS data
  95. AWSAccessKeyID string `json:"aws_access_key_id"`
  96. AWSSecretAccessKey string `json:"aws_secret_access_key"`
  97. AWSClusterID string `json:"aws_cluster_id"`
  98. // OIDC-related fields
  99. OIDCIssuerURL string `json:"idp-issuer-url"`
  100. OIDCClientID string `json:"client-id"`
  101. OIDCClientSecret string `json:"client-secret"`
  102. OIDCCertificateAuthorityData string `json:"idp-certificate-authority-data"`
  103. OIDCIDToken string `json:"id-token"`
  104. OIDCRefreshToken string `json:"refresh-token"`
  105. }
  106. // ServiceAccountExternal is an external ServiceAccount to be shared over REST
  107. type ServiceAccountExternal struct {
  108. ID uint `json:"id"`
  109. ProjectID uint `json:"project_id"`
  110. Kind string `json:"kind"`
  111. Clusters []ClusterExternal `json:"clusters"`
  112. AuthMechanism string `json:"auth_mechanism"`
  113. }
  114. // Externalize generates an external ServiceAccount to be shared over REST
  115. func (s *ServiceAccount) Externalize() *ServiceAccountExternal {
  116. clusters := make([]ClusterExternal, 0)
  117. for _, cluster := range s.Clusters {
  118. clusters = append(clusters, *cluster.Externalize())
  119. }
  120. return &ServiceAccountExternal{
  121. ID: s.ID,
  122. ProjectID: s.ProjectID,
  123. Kind: s.Kind,
  124. Clusters: clusters,
  125. AuthMechanism: s.AuthMechanism,
  126. }
  127. }