cluster.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. } else if c.DOIntegrationID != 0 {
  77. serv = integrations.DOKS
  78. }
  79. return &ClusterExternal{
  80. ID: c.ID,
  81. ProjectID: c.ProjectID,
  82. Name: c.Name,
  83. Server: c.Server,
  84. Service: serv,
  85. InfraID: c.InfraID,
  86. }
  87. }
  88. // ClusterCandidate is a cluster integration that requires additional action
  89. // from the user to set up.
  90. type ClusterCandidate struct {
  91. gorm.Model
  92. // The auth mechanism that this candidate will parse for
  93. AuthMechanism ClusterAuth `json:"auth_mechanism"`
  94. // The project that this integration belongs to
  95. ProjectID uint `json:"project_id"`
  96. // CreatedClusterID is the ID of the cluster that's eventually
  97. // created
  98. CreatedClusterID uint `json:"created_cluster_id"`
  99. // Resolvers are the list of resolvers: once all resolvers are "resolved," the
  100. // cluster will be created
  101. Resolvers []ClusterResolver `json:"resolvers"`
  102. // Name of the cluster
  103. Name string `json:"name"`
  104. // Server endpoint for the cluster
  105. Server string `json:"server"`
  106. // Name of the context that this was created from, if it exists
  107. ContextName string `json:"context_name"`
  108. // ------------------------------------------------------------------
  109. // All fields below this line are encrypted before storage
  110. // ------------------------------------------------------------------
  111. // The best-guess for the AWSClusterID, which is required by aws auth mechanisms
  112. // See https://github.com/kubernetes-sigs/aws-iam-authenticator#what-is-a-cluster-id
  113. AWSClusterIDGuess []byte `json:"aws_cluster_id_guess"`
  114. // The raw kubeconfig
  115. Kubeconfig []byte `json:"kubeconfig"`
  116. }
  117. // ClusterCandidateExternal represents the ClusterCandidate to be sent over REST
  118. type ClusterCandidateExternal struct {
  119. ID uint `json:"id"`
  120. // The project that this integration belongs to
  121. ProjectID uint `json:"project_id"`
  122. // CreatedClusterID is the ID of the cluster that's eventually
  123. // created
  124. CreatedClusterID uint `json:"created_cluster_id"`
  125. // Name of the cluster
  126. Name string `json:"name"`
  127. // Server endpoint for the cluster
  128. Server string `json:"server"`
  129. // Name of the context that this was created from, if it exists
  130. ContextName string `json:"context_name"`
  131. // Resolvers are the list of resolvers: once all resolvers are "resolved," the
  132. // cluster will be created
  133. Resolvers []ClusterResolverExternal `json:"resolvers"`
  134. // The best-guess for the AWSClusterID, which is required by aws auth mechanisms
  135. // See https://github.com/kubernetes-sigs/aws-iam-authenticator#what-is-a-cluster-id
  136. AWSClusterIDGuess string `json:"aws_cluster_id_guess"`
  137. }
  138. // Externalize generates an external ClusterCandidateExternal to be shared over REST
  139. func (cc *ClusterCandidate) Externalize() *ClusterCandidateExternal {
  140. resolvers := make([]ClusterResolverExternal, 0)
  141. for _, resolver := range cc.Resolvers {
  142. resolvers = append(resolvers, *resolver.Externalize())
  143. }
  144. return &ClusterCandidateExternal{
  145. ID: cc.ID,
  146. ProjectID: cc.ProjectID,
  147. CreatedClusterID: cc.CreatedClusterID,
  148. Name: cc.Name,
  149. Server: cc.Server,
  150. ContextName: cc.ContextName,
  151. Resolvers: resolvers,
  152. AWSClusterIDGuess: string(cc.AWSClusterIDGuess),
  153. }
  154. }
  155. // ClusterResolverName is the name for a cluster resolve
  156. type ClusterResolverName string
  157. // Options for the cluster resolver names
  158. const (
  159. ClusterCAData ClusterResolverName = "upload-cluster-ca-data"
  160. ClusterLocalhost = "rewrite-cluster-localhost"
  161. ClientCertData = "upload-client-cert-data"
  162. ClientKeyData = "upload-client-key-data"
  163. OIDCIssuerData = "upload-oidc-idp-issuer-ca-data"
  164. TokenData = "upload-token-data"
  165. GCPKeyData = "upload-gcp-key-data"
  166. AWSData = "upload-aws-data"
  167. )
  168. // ClusterResolverInfo contains the information for actions to be
  169. // performed in order to initialize a cluster
  170. type ClusterResolverInfo struct {
  171. // Docs is a link to documentation that helps resolve this manually
  172. Docs string `json:"docs"`
  173. // a comma-separated list of required fields to send in an action request
  174. Fields string `json:"fields"`
  175. }
  176. // ClusterResolverInfos is a map of the information for actions to be
  177. // performed in order to initialize a cluster
  178. var ClusterResolverInfos = map[ClusterResolverName]ClusterResolverInfo{
  179. ClusterCAData: ClusterResolverInfo{
  180. Docs: "https://github.com/porter-dev/porter",
  181. Fields: "cluster_ca_data",
  182. },
  183. ClusterLocalhost: ClusterResolverInfo{
  184. Docs: "https://github.com/porter-dev/porter",
  185. Fields: "cluster_hostname",
  186. },
  187. ClientCertData: ClusterResolverInfo{
  188. Docs: "https://github.com/porter-dev/porter",
  189. Fields: "client_cert_data",
  190. },
  191. ClientKeyData: ClusterResolverInfo{
  192. Docs: "https://github.com/porter-dev/porter",
  193. Fields: "client_key_data",
  194. },
  195. OIDCIssuerData: ClusterResolverInfo{
  196. Docs: "https://github.com/porter-dev/porter",
  197. Fields: "oidc_idp_issuer_ca_data",
  198. },
  199. TokenData: ClusterResolverInfo{
  200. Docs: "https://github.com/porter-dev/porter",
  201. Fields: "token_data",
  202. },
  203. GCPKeyData: ClusterResolverInfo{
  204. Docs: "https://github.com/porter-dev/porter",
  205. Fields: "gcp_key_data",
  206. },
  207. AWSData: ClusterResolverInfo{
  208. Docs: "https://github.com/porter-dev/porter",
  209. Fields: "aws_access_key_id,aws_secret_access_key,aws_cluster_id",
  210. },
  211. }
  212. // ClusterResolverAll is a helper type that contains the fields for
  213. // all possible resolvers, so that raw bytes can be unmarshaled in a single
  214. // read
  215. type ClusterResolverAll struct {
  216. ClusterCAData string `json:"cluster_ca_data,omitempty"`
  217. ClusterHostname string `json:"cluster_hostname,omitempty"`
  218. ClientCertData string `json:"client_cert_data,omitempty"`
  219. ClientKeyData string `json:"client_key_data,omitempty"`
  220. OIDCIssuerCAData string `json:"oidc_idp_issuer_ca_data,omitempty"`
  221. TokenData string `json:"token_data,omitempty"`
  222. GCPKeyData string `json:"gcp_key_data,omitempty"`
  223. AWSAccessKeyID string `json:"aws_access_key_id"`
  224. AWSSecretAccessKey string `json:"aws_secret_access_key"`
  225. AWSClusterID string `json:"aws_cluster_id"`
  226. }
  227. // ClusterResolver is an action that must be resolved to set up
  228. // a Cluster
  229. type ClusterResolver struct {
  230. gorm.Model
  231. // The ClusterCandidate that this is resolving
  232. ClusterCandidateID uint `json:"cluster_candidate_id"`
  233. // One of the ClusterResolverNames
  234. Name ClusterResolverName `json:"name"`
  235. // Resolved is true if this has been resolved, false otherwise
  236. Resolved bool `json:"resolved"`
  237. // Data is additional data for resolving the action, for example a file name,
  238. // context name, etc
  239. Data []byte `json:"data,omitempty"`
  240. }
  241. // ClusterResolverData is a map of key names to fields, which gets marshaled from
  242. // the raw JSON bytes stored in the ClusterResolver
  243. type ClusterResolverData map[string]string
  244. // ClusterResolverExternal is an external ClusterResolver to be shared over REST
  245. type ClusterResolverExternal struct {
  246. ID uint `json:"id"`
  247. // The ClusterCandidate that this is resolving
  248. ClusterCandidateID uint `json:"cluster_candidate_id"`
  249. // One of the ClusterResolverNames
  250. Name ClusterResolverName `json:"name"`
  251. // Resolved is true if this has been resolved, false otherwise
  252. Resolved bool `json:"resolved"`
  253. // Docs is a link to documentation that helps resolve this manually
  254. Docs string `json:"docs"`
  255. // Fields is a list of fields that must be sent with the resolving request
  256. Fields string `json:"fields"`
  257. // Data is additional data for resolving the action, for example a file name,
  258. // context name, etc
  259. Data ClusterResolverData `json:"data,omitempty"`
  260. }
  261. // Externalize generates an external ClusterResolver to be shared over REST
  262. func (cr *ClusterResolver) Externalize() *ClusterResolverExternal {
  263. info := ClusterResolverInfos[cr.Name]
  264. data := make(ClusterResolverData)
  265. json.Unmarshal(cr.Data, &data)
  266. return &ClusterResolverExternal{
  267. ID: cr.ID,
  268. ClusterCandidateID: cr.ClusterCandidateID,
  269. Name: cr.Name,
  270. Resolved: cr.Resolved,
  271. Docs: info.Docs,
  272. Fields: info.Fields,
  273. Data: data,
  274. }
  275. }