cluster.go 11 KB

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