cluster.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package models
  2. import (
  3. "encoding/json"
  4. "github.com/porter-dev/porter/internal/models/integrations"
  5. "gorm.io/gorm"
  6. )
  7. // ClusterAuth is an auth mechanism that a cluster candidate can resolve
  8. type ClusterAuth string
  9. // The support cluster candidate auth mechanisms
  10. const (
  11. X509 ClusterAuth = "x509"
  12. Basic ClusterAuth = "basic"
  13. Bearer ClusterAuth = "bearerToken"
  14. OIDC ClusterAuth = "oidc"
  15. GCP ClusterAuth = "gcp-sa"
  16. AWS ClusterAuth = "aws-sa"
  17. Local ClusterAuth = "local"
  18. )
  19. // Cluster is an integration that can connect to a Kubernetes cluster via
  20. // a specific auth mechanism
  21. type Cluster struct {
  22. gorm.Model
  23. // The auth mechanism that this cluster will use
  24. AuthMechanism ClusterAuth `json:"auth_mechanism"`
  25. // The project that this integration belongs to
  26. ProjectID uint `json:"project_id"`
  27. // Name of the cluster
  28. Name string `json:"name"`
  29. // Server endpoint for the cluster
  30. Server string `json:"server"`
  31. // Additional fields optionally used by the kube client
  32. ClusterLocationOfOrigin string `json:"location_of_origin,omitempty"`
  33. TLSServerName string `json:"tls-server-name,omitempty"`
  34. InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify,omitempty"`
  35. ProxyURL string `json:"proxy-url,omitempty"`
  36. UserLocationOfOrigin string
  37. UserImpersonate string `json:"act-as,omitempty"`
  38. UserImpersonateGroups string `json:"act-as-groups,omitempty"`
  39. InfraID uint `json:"infra_id"`
  40. // ------------------------------------------------------------------
  41. // All fields below this line are encrypted before storage
  42. // ------------------------------------------------------------------
  43. // The various auth mechanisms available to the integration
  44. KubeIntegrationID uint
  45. OIDCIntegrationID uint
  46. GCPIntegrationID uint
  47. AWSIntegrationID uint
  48. // A token cache that can be used by an auth mechanism, if desired
  49. TokenCache integrations.ClusterTokenCache `json:"token_cache"`
  50. // CertificateAuthorityData for the cluster, encrypted at rest
  51. CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"`
  52. }
  53. // ClusterExternal is an external Cluster to be shared over REST
  54. type ClusterExternal struct {
  55. ID uint `json:"id"`
  56. // The project that this integration belongs to
  57. ProjectID uint `json:"project_id"`
  58. // Name of the cluster
  59. Name string `json:"name"`
  60. // Server endpoint for the cluster
  61. Server string `json:"server"`
  62. // The integration service for this cluster
  63. Service integrations.IntegrationService `json:"service"`
  64. // The infra id, if cluster was provisioned with Porter
  65. InfraID uint `json:"infra_id"`
  66. }
  67. // Externalize generates an external Cluster to be shared over REST
  68. func (c *Cluster) Externalize() *ClusterExternal {
  69. serv := integrations.Kube
  70. if c.AWSIntegrationID != 0 {
  71. serv = integrations.EKS
  72. } else if c.GCPIntegrationID != 0 {
  73. serv = integrations.GKE
  74. }
  75. return &ClusterExternal{
  76. ID: c.ID,
  77. ProjectID: c.ProjectID,
  78. Name: c.Name,
  79. Server: c.Server,
  80. Service: serv,
  81. InfraID: c.InfraID,
  82. }
  83. }
  84. // ClusterCandidate is a cluster integration that requires additional action
  85. // from the user to set up.
  86. type ClusterCandidate struct {
  87. gorm.Model
  88. // The auth mechanism that this candidate will parse for
  89. AuthMechanism ClusterAuth `json:"auth_mechanism"`
  90. // The project that this integration belongs to
  91. ProjectID uint `json:"project_id"`
  92. // CreatedClusterID is the ID of the cluster that's eventually
  93. // created
  94. CreatedClusterID uint `json:"created_cluster_id"`
  95. // Resolvers are the list of resolvers: once all resolvers are "resolved," the
  96. // cluster will be created
  97. Resolvers []ClusterResolver `json:"resolvers"`
  98. // Name of the cluster
  99. Name string `json:"name"`
  100. // Server endpoint for the cluster
  101. Server string `json:"server"`
  102. // Name of the context that this was created from, if it exists
  103. ContextName string `json:"context_name"`
  104. // ------------------------------------------------------------------
  105. // All fields below this line are encrypted before storage
  106. // ------------------------------------------------------------------
  107. // The best-guess for the AWSClusterID, which is required by aws auth mechanisms
  108. // See https://github.com/kubernetes-sigs/aws-iam-authenticator#what-is-a-cluster-id
  109. AWSClusterIDGuess []byte `json:"aws_cluster_id_guess"`
  110. // The raw kubeconfig
  111. Kubeconfig []byte `json:"kubeconfig"`
  112. }
  113. // ClusterCandidateExternal represents the ClusterCandidate to be sent over REST
  114. type ClusterCandidateExternal struct {
  115. ID uint `json:"id"`
  116. // The project that this integration belongs to
  117. ProjectID uint `json:"project_id"`
  118. // CreatedClusterID is the ID of the cluster that's eventually
  119. // created
  120. CreatedClusterID uint `json:"created_cluster_id"`
  121. // Name of the cluster
  122. Name string `json:"name"`
  123. // Server endpoint for the cluster
  124. Server string `json:"server"`
  125. // Name of the context that this was created from, if it exists
  126. ContextName string `json:"context_name"`
  127. // Resolvers are the list of resolvers: once all resolvers are "resolved," the
  128. // cluster will be created
  129. Resolvers []ClusterResolverExternal `json:"resolvers"`
  130. // The best-guess for the AWSClusterID, which is required by aws auth mechanisms
  131. // See https://github.com/kubernetes-sigs/aws-iam-authenticator#what-is-a-cluster-id
  132. AWSClusterIDGuess string `json:"aws_cluster_id_guess"`
  133. }
  134. // Externalize generates an external ClusterCandidateExternal to be shared over REST
  135. func (cc *ClusterCandidate) Externalize() *ClusterCandidateExternal {
  136. resolvers := make([]ClusterResolverExternal, 0)
  137. for _, resolver := range cc.Resolvers {
  138. resolvers = append(resolvers, *resolver.Externalize())
  139. }
  140. return &ClusterCandidateExternal{
  141. ID: cc.ID,
  142. ProjectID: cc.ProjectID,
  143. CreatedClusterID: cc.CreatedClusterID,
  144. Name: cc.Name,
  145. Server: cc.Server,
  146. ContextName: cc.ContextName,
  147. Resolvers: resolvers,
  148. AWSClusterIDGuess: string(cc.AWSClusterIDGuess),
  149. }
  150. }
  151. // ClusterResolverName is the name for a cluster resolve
  152. type ClusterResolverName string
  153. // Options for the cluster resolver names
  154. const (
  155. ClusterCAData ClusterResolverName = "upload-cluster-ca-data"
  156. ClusterLocalhost = "rewrite-cluster-localhost"
  157. ClientCertData = "upload-client-cert-data"
  158. ClientKeyData = "upload-client-key-data"
  159. OIDCIssuerData = "upload-oidc-idp-issuer-ca-data"
  160. TokenData = "upload-token-data"
  161. GCPKeyData = "upload-gcp-key-data"
  162. AWSData = "upload-aws-data"
  163. )
  164. // ClusterResolverInfo contains the information for actions to be
  165. // performed in order to initialize a cluster
  166. type ClusterResolverInfo struct {
  167. // Docs is a link to documentation that helps resolve this manually
  168. Docs string `json:"docs"`
  169. // a comma-separated list of required fields to send in an action request
  170. Fields string `json:"fields"`
  171. }
  172. // ClusterResolverInfos is a map of the information for actions to be
  173. // performed in order to initialize a cluster
  174. var ClusterResolverInfos = map[ClusterResolverName]ClusterResolverInfo{
  175. ClusterCAData: ClusterResolverInfo{
  176. Docs: "https://github.com/porter-dev/porter",
  177. Fields: "cluster_ca_data",
  178. },
  179. ClusterLocalhost: ClusterResolverInfo{
  180. Docs: "https://github.com/porter-dev/porter",
  181. Fields: "cluster_hostname",
  182. },
  183. ClientCertData: ClusterResolverInfo{
  184. Docs: "https://github.com/porter-dev/porter",
  185. Fields: "client_cert_data",
  186. },
  187. ClientKeyData: ClusterResolverInfo{
  188. Docs: "https://github.com/porter-dev/porter",
  189. Fields: "client_key_data",
  190. },
  191. OIDCIssuerData: ClusterResolverInfo{
  192. Docs: "https://github.com/porter-dev/porter",
  193. Fields: "oidc_idp_issuer_ca_data",
  194. },
  195. TokenData: ClusterResolverInfo{
  196. Docs: "https://github.com/porter-dev/porter",
  197. Fields: "token_data",
  198. },
  199. GCPKeyData: ClusterResolverInfo{
  200. Docs: "https://github.com/porter-dev/porter",
  201. Fields: "gcp_key_data",
  202. },
  203. AWSData: ClusterResolverInfo{
  204. Docs: "https://github.com/porter-dev/porter",
  205. Fields: "aws_access_key_id,aws_secret_access_key,aws_cluster_id",
  206. },
  207. }
  208. // ClusterResolverAll is a helper type that contains the fields for
  209. // all possible resolvers, so that raw bytes can be unmarshaled in a single
  210. // read
  211. type ClusterResolverAll struct {
  212. ClusterCAData string `json:"cluster_ca_data,omitempty"`
  213. ClusterHostname string `json:"cluster_hostname,omitempty"`
  214. ClientCertData string `json:"client_cert_data,omitempty"`
  215. ClientKeyData string `json:"client_key_data,omitempty"`
  216. OIDCIssuerCAData string `json:"oidc_idp_issuer_ca_data,omitempty"`
  217. TokenData string `json:"token_data,omitempty"`
  218. GCPKeyData string `json:"gcp_key_data,omitempty"`
  219. AWSAccessKeyID string `json:"aws_access_key_id"`
  220. AWSSecretAccessKey string `json:"aws_secret_access_key"`
  221. AWSClusterID string `json:"aws_cluster_id"`
  222. }
  223. // ClusterResolver is an action that must be resolved to set up
  224. // a Cluster
  225. type ClusterResolver struct {
  226. gorm.Model
  227. // The ClusterCandidate that this is resolving
  228. ClusterCandidateID uint `json:"cluster_candidate_id"`
  229. // One of the ClusterResolverNames
  230. Name ClusterResolverName `json:"name"`
  231. // Resolved is true if this has been resolved, false otherwise
  232. Resolved bool `json:"resolved"`
  233. // Data is additional data for resolving the action, for example a file name,
  234. // context name, etc
  235. Data []byte `json:"data,omitempty"`
  236. }
  237. // ClusterResolverData is a map of key names to fields, which gets marshaled from
  238. // the raw JSON bytes stored in the ClusterResolver
  239. type ClusterResolverData map[string]string
  240. // ClusterResolverExternal is an external ClusterResolver to be shared over REST
  241. type ClusterResolverExternal struct {
  242. ID uint `json:"id"`
  243. // The ClusterCandidate that this is resolving
  244. ClusterCandidateID uint `json:"cluster_candidate_id"`
  245. // One of the ClusterResolverNames
  246. Name ClusterResolverName `json:"name"`
  247. // Resolved is true if this has been resolved, false otherwise
  248. Resolved bool `json:"resolved"`
  249. // Docs is a link to documentation that helps resolve this manually
  250. Docs string `json:"docs"`
  251. // Fields is a list of fields that must be sent with the resolving request
  252. Fields string `json:"fields"`
  253. // Data is additional data for resolving the action, for example a file name,
  254. // context name, etc
  255. Data ClusterResolverData `json:"data,omitempty"`
  256. }
  257. // Externalize generates an external ClusterResolver to be shared over REST
  258. func (cr *ClusterResolver) Externalize() *ClusterResolverExternal {
  259. info := ClusterResolverInfos[cr.Name]
  260. data := make(ClusterResolverData)
  261. json.Unmarshal(cr.Data, &data)
  262. return &ClusterResolverExternal{
  263. ID: cr.ID,
  264. ClusterCandidateID: cr.ClusterCandidateID,
  265. Name: cr.Name,
  266. Resolved: cr.Resolved,
  267. Docs: info.Docs,
  268. Fields: info.Fields,
  269. Data: data,
  270. }
  271. }