cluster.go 11 KB

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