cluster.go 11 KB

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