cluster.go 12 KB

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