cluster.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. package forms
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "fmt"
  6. "net/url"
  7. "regexp"
  8. "strings"
  9. "github.com/porter-dev/porter/internal/kubernetes"
  10. "github.com/porter-dev/porter/internal/models"
  11. "github.com/porter-dev/porter/internal/repository"
  12. "k8s.io/client-go/tools/clientcmd/api"
  13. ints "github.com/porter-dev/porter/internal/models/integrations"
  14. )
  15. // CreateClusterForm represents the accepted values for creating a
  16. // cluster through manual configuration (not through a kubeconfig)
  17. type CreateClusterForm struct {
  18. Name string `json:"name" form:"required"`
  19. ProjectID uint `json:"project_id" form:"required"`
  20. Server string `json:"server" form:"required"`
  21. GCPIntegrationID uint `json:"gcp_integration_id"`
  22. AWSIntegrationID uint `json:"aws_integration_id"`
  23. CertificateAuthorityData string `json:"certificate_authority_data,omitempty"`
  24. }
  25. // ToCluster converts the form to a cluster
  26. func (ccf *CreateClusterForm) ToCluster() (*models.Cluster, error) {
  27. var authMechanism models.ClusterAuth
  28. if ccf.GCPIntegrationID != 0 {
  29. authMechanism = models.GCP
  30. } else if ccf.AWSIntegrationID != 0 {
  31. authMechanism = models.AWS
  32. } else {
  33. return nil, fmt.Errorf("must include aws or gcp integration id")
  34. }
  35. cert := make([]byte, 0)
  36. if ccf.CertificateAuthorityData != "" {
  37. // determine if data is base64 decoded using regex
  38. re := regexp.MustCompile(`^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$`)
  39. // if it matches the base64 regex, decode it
  40. if re.MatchString(ccf.CertificateAuthorityData) {
  41. decoded, err := base64.StdEncoding.DecodeString(ccf.CertificateAuthorityData)
  42. if err != nil {
  43. return nil, err
  44. }
  45. cert = []byte(decoded)
  46. }
  47. }
  48. return &models.Cluster{
  49. AuthMechanism: authMechanism,
  50. Name: ccf.Name,
  51. Server: ccf.Server,
  52. GCPIntegrationID: ccf.GCPIntegrationID,
  53. AWSIntegrationID: ccf.AWSIntegrationID,
  54. CertificateAuthorityData: cert,
  55. }, nil
  56. }
  57. // UpdateClusterForm represents the accepted values for updating a
  58. // cluster (only name for now)
  59. type UpdateClusterForm struct {
  60. ID uint
  61. Name string `json:"name" form:"required"`
  62. }
  63. // ToCluster converts the form to a cluster
  64. func (ucf *UpdateClusterForm) ToCluster(repo repository.ClusterRepository) (*models.Cluster, error) {
  65. cluster, err := repo.ReadCluster(ucf.ID)
  66. if err != nil {
  67. return nil, err
  68. }
  69. cluster.Name = ucf.Name
  70. return cluster, nil
  71. }
  72. // ResolveClusterForm will resolve a cluster candidate and create a new cluster
  73. type ResolveClusterForm struct {
  74. Resolver *models.ClusterResolverAll `form:"required"`
  75. ClusterCandidateID uint `json:"cluster_candidate_id" form:"required"`
  76. ProjectID uint `json:"project_id" form:"required"`
  77. UserID uint `json:"user_id" form:"required"`
  78. // populated during the ResolveIntegration step
  79. IntegrationID uint
  80. ClusterCandidate *models.ClusterCandidate
  81. RawConf *api.Config
  82. }
  83. // ResolveIntegration creates an integration in the DB
  84. func (rcf *ResolveClusterForm) ResolveIntegration(
  85. repo repository.Repository,
  86. ) error {
  87. cc, err := repo.Cluster.ReadClusterCandidate(rcf.ClusterCandidateID)
  88. if err != nil {
  89. return err
  90. }
  91. rcf.ClusterCandidate = cc
  92. rawConf, err := kubernetes.GetRawConfigFromBytes(cc.Kubeconfig)
  93. if err != nil {
  94. return err
  95. }
  96. rcf.RawConf = rawConf
  97. context := rawConf.Contexts[rawConf.CurrentContext]
  98. authInfoName := context.AuthInfo
  99. authInfo := rawConf.AuthInfos[authInfoName]
  100. // iterate through the resolvers, and use the ClusterResolverAll to populate
  101. // the required fields
  102. var id uint
  103. switch cc.AuthMechanism {
  104. case models.X509:
  105. id, err = rcf.resolveX509(repo, authInfo)
  106. case models.Bearer:
  107. id, err = rcf.resolveToken(repo, authInfo)
  108. case models.Basic:
  109. id, err = rcf.resolveBasic(repo, authInfo)
  110. case models.Local:
  111. id, err = rcf.resolveLocal(repo, authInfo)
  112. case models.OIDC:
  113. id, err = rcf.resolveOIDC(repo, authInfo)
  114. case models.GCP:
  115. id, err = rcf.resolveGCP(repo, authInfo)
  116. case models.AWS:
  117. id, err = rcf.resolveAWS(repo, authInfo)
  118. }
  119. if err != nil {
  120. return err
  121. }
  122. rcf.IntegrationID = id
  123. return nil
  124. }
  125. func (rcf *ResolveClusterForm) resolveX509(
  126. repo repository.Repository,
  127. authInfo *api.AuthInfo,
  128. ) (uint, error) {
  129. ki := &ints.KubeIntegration{
  130. Mechanism: ints.KubeX509,
  131. UserID: rcf.UserID,
  132. ProjectID: rcf.ProjectID,
  133. }
  134. // attempt to construct cert and key from raw config
  135. if len(authInfo.ClientCertificateData) > 0 {
  136. ki.ClientCertificateData = authInfo.ClientCertificateData
  137. }
  138. if len(authInfo.ClientKeyData) > 0 {
  139. ki.ClientKeyData = authInfo.ClientKeyData
  140. }
  141. // override with resolver
  142. if rcf.Resolver.ClientCertData != "" {
  143. decoded, err := base64.StdEncoding.DecodeString(rcf.Resolver.ClientCertData)
  144. if err != nil {
  145. return 0, err
  146. }
  147. ki.ClientCertificateData = decoded
  148. }
  149. if rcf.Resolver.ClientKeyData != "" {
  150. decoded, err := base64.StdEncoding.DecodeString(rcf.Resolver.ClientKeyData)
  151. if err != nil {
  152. return 0, err
  153. }
  154. ki.ClientKeyData = decoded
  155. }
  156. // if resolvable, write kube integration to repo
  157. if len(ki.ClientCertificateData) == 0 || len(ki.ClientKeyData) == 0 {
  158. return 0, errors.New("could not resolve kube integration (x509)")
  159. }
  160. // return integration id if exists
  161. ki, err := repo.KubeIntegration.CreateKubeIntegration(ki)
  162. if err != nil {
  163. return 0, err
  164. }
  165. return ki.Model.ID, nil
  166. }
  167. func (rcf *ResolveClusterForm) resolveToken(
  168. repo repository.Repository,
  169. authInfo *api.AuthInfo,
  170. ) (uint, error) {
  171. ki := &ints.KubeIntegration{
  172. Mechanism: ints.KubeBearer,
  173. UserID: rcf.UserID,
  174. ProjectID: rcf.ProjectID,
  175. }
  176. // attempt to construct token from raw config
  177. if len(authInfo.Token) > 0 {
  178. ki.Token = []byte(authInfo.Token)
  179. }
  180. // supplement with resolver
  181. if rcf.Resolver.TokenData != "" {
  182. ki.Token = []byte(rcf.Resolver.TokenData)
  183. }
  184. // if resolvable, write kube integration to repo
  185. if len(ki.Token) == 0 {
  186. return 0, errors.New("could not resolve kube integration (token)")
  187. }
  188. // return integration id if exists
  189. ki, err := repo.KubeIntegration.CreateKubeIntegration(ki)
  190. if err != nil {
  191. return 0, err
  192. }
  193. return ki.Model.ID, nil
  194. }
  195. func (rcf *ResolveClusterForm) resolveBasic(
  196. repo repository.Repository,
  197. authInfo *api.AuthInfo,
  198. ) (uint, error) {
  199. ki := &ints.KubeIntegration{
  200. Mechanism: ints.KubeBasic,
  201. UserID: rcf.UserID,
  202. ProjectID: rcf.ProjectID,
  203. }
  204. if len(authInfo.Username) > 0 {
  205. ki.Username = []byte(authInfo.Username)
  206. }
  207. if len(authInfo.Password) > 0 {
  208. ki.Password = []byte(authInfo.Password)
  209. }
  210. if len(ki.Username) == 0 || len(ki.Password) == 0 {
  211. return 0, errors.New("could not resolve kube integration (basic)")
  212. }
  213. // return integration id if exists
  214. ki, err := repo.KubeIntegration.CreateKubeIntegration(ki)
  215. if err != nil {
  216. return 0, err
  217. }
  218. return ki.Model.ID, nil
  219. }
  220. func (rcf *ResolveClusterForm) resolveLocal(
  221. repo repository.Repository,
  222. authInfo *api.AuthInfo,
  223. ) (uint, error) {
  224. ki := &ints.KubeIntegration{
  225. Mechanism: ints.KubeLocal,
  226. UserID: rcf.UserID,
  227. ProjectID: rcf.ProjectID,
  228. Kubeconfig: rcf.ClusterCandidate.Kubeconfig,
  229. }
  230. // return integration id if exists
  231. ki, err := repo.KubeIntegration.CreateKubeIntegration(ki)
  232. if err != nil {
  233. return 0, err
  234. }
  235. return ki.Model.ID, nil
  236. }
  237. func (rcf *ResolveClusterForm) resolveOIDC(
  238. repo repository.Repository,
  239. authInfo *api.AuthInfo,
  240. ) (uint, error) {
  241. oidc := &ints.OIDCIntegration{
  242. Client: ints.OIDCKube,
  243. UserID: rcf.UserID,
  244. ProjectID: rcf.ProjectID,
  245. }
  246. if url, ok := authInfo.AuthProvider.Config["idp-issuer-url"]; ok {
  247. oidc.IssuerURL = []byte(url)
  248. }
  249. if clientID, ok := authInfo.AuthProvider.Config["client-id"]; ok {
  250. oidc.ClientID = []byte(clientID)
  251. }
  252. if clientSecret, ok := authInfo.AuthProvider.Config["client-secret"]; ok {
  253. oidc.ClientSecret = []byte(clientSecret)
  254. }
  255. if caData, ok := authInfo.AuthProvider.Config["idp-certificate-authority-data"]; ok {
  256. // based on the implementation, the oidc plugin expects the data to be base64 encoded,
  257. // which means we will not decode it here
  258. // reference: https://github.com/kubernetes/kubernetes/blob/9dfb4c876bfca7a5ae84259fae2bc337ed90c2d7/staging/src/k8s.io/client-go/plugin/pkg/client/auth/oidc/oidc.go#L135
  259. oidc.CertificateAuthorityData = []byte(caData)
  260. }
  261. if idToken, ok := authInfo.AuthProvider.Config["id-token"]; ok {
  262. oidc.IDToken = []byte(idToken)
  263. }
  264. if refreshToken, ok := authInfo.AuthProvider.Config["refresh-token"]; ok {
  265. oidc.RefreshToken = []byte(refreshToken)
  266. }
  267. // override with resolver
  268. if rcf.Resolver.OIDCIssuerCAData != "" {
  269. // based on the implementation, the oidc plugin expects the data to be base64 encoded,
  270. // which means we will not decode it here
  271. // reference: https://github.com/kubernetes/kubernetes/blob/9dfb4c876bfca7a5ae84259fae2bc337ed90c2d7/staging/src/k8s.io/client-go/plugin/pkg/client/auth/oidc/oidc.go#L135
  272. oidc.CertificateAuthorityData = []byte(rcf.Resolver.OIDCIssuerCAData)
  273. }
  274. // return integration id if exists
  275. oidc, err := repo.OIDCIntegration.CreateOIDCIntegration(oidc)
  276. if err != nil {
  277. return 0, err
  278. }
  279. return oidc.Model.ID, nil
  280. }
  281. func (rcf *ResolveClusterForm) resolveGCP(
  282. repo repository.Repository,
  283. authInfo *api.AuthInfo,
  284. ) (uint, error) {
  285. // TODO -- add GCP project ID and GCP email so that source is trackable
  286. gcp := &ints.GCPIntegration{
  287. UserID: rcf.UserID,
  288. ProjectID: rcf.ProjectID,
  289. }
  290. // supplement with resolver
  291. if rcf.Resolver.GCPKeyData != "" {
  292. gcp.GCPKeyData = []byte(rcf.Resolver.GCPKeyData)
  293. }
  294. // throw error if no data
  295. if len(gcp.GCPKeyData) == 0 {
  296. return 0, errors.New("could not resolve gcp integration")
  297. }
  298. // return integration id if exists
  299. gcp, err := repo.GCPIntegration.CreateGCPIntegration(gcp)
  300. if err != nil {
  301. return 0, err
  302. }
  303. return gcp.Model.ID, nil
  304. }
  305. func (rcf *ResolveClusterForm) resolveAWS(
  306. repo repository.Repository,
  307. authInfo *api.AuthInfo,
  308. ) (uint, error) {
  309. // TODO -- add AWS session token as an optional param
  310. // TODO -- add AWS entity and user ARN
  311. aws := &ints.AWSIntegration{
  312. UserID: rcf.UserID,
  313. ProjectID: rcf.ProjectID,
  314. }
  315. // override with resolver
  316. if rcf.Resolver.AWSClusterID != "" {
  317. aws.AWSClusterID = []byte(rcf.Resolver.AWSClusterID)
  318. }
  319. if rcf.Resolver.AWSAccessKeyID != "" {
  320. aws.AWSAccessKeyID = []byte(rcf.Resolver.AWSAccessKeyID)
  321. }
  322. if rcf.Resolver.AWSSecretAccessKey != "" {
  323. aws.AWSSecretAccessKey = []byte(rcf.Resolver.AWSSecretAccessKey)
  324. }
  325. // throw error if no data
  326. if len(aws.AWSClusterID) == 0 || len(aws.AWSAccessKeyID) == 0 || len(aws.AWSSecretAccessKey) == 0 {
  327. return 0, errors.New("could not resolve aws integration")
  328. }
  329. // return integration id if exists
  330. aws, err := repo.AWSIntegration.CreateAWSIntegration(aws)
  331. if err != nil {
  332. return 0, err
  333. }
  334. return aws.Model.ID, nil
  335. }
  336. // ResolveCluster writes a new cluster to the DB -- this must be called after
  337. // rcf.ResolveIntegration, since it relies on the previously created integration.
  338. func (rcf *ResolveClusterForm) ResolveCluster(
  339. repo repository.Repository,
  340. ) (*models.Cluster, error) {
  341. // build a cluster from the candidate
  342. cluster, err := rcf.buildCluster()
  343. if err != nil {
  344. return nil, err
  345. }
  346. // save cluster to db
  347. return repo.Cluster.CreateCluster(cluster)
  348. }
  349. func (rcf *ResolveClusterForm) buildCluster() (*models.Cluster, error) {
  350. rawConf := rcf.RawConf
  351. kcContext := rawConf.Contexts[rawConf.CurrentContext]
  352. kcAuthInfoName := kcContext.AuthInfo
  353. kcAuthInfo := rawConf.AuthInfos[kcAuthInfoName]
  354. kcClusterName := kcContext.Cluster
  355. kcCluster := rawConf.Clusters[kcClusterName]
  356. cc := rcf.ClusterCandidate
  357. cluster := &models.Cluster{
  358. AuthMechanism: cc.AuthMechanism,
  359. ProjectID: cc.ProjectID,
  360. Name: cc.Name,
  361. Server: cc.Server,
  362. ClusterLocationOfOrigin: kcCluster.LocationOfOrigin,
  363. TLSServerName: kcCluster.TLSServerName,
  364. InsecureSkipTLSVerify: kcCluster.InsecureSkipTLSVerify,
  365. UserLocationOfOrigin: kcAuthInfo.LocationOfOrigin,
  366. UserImpersonate: kcAuthInfo.Impersonate,
  367. }
  368. if len(kcAuthInfo.ImpersonateGroups) > 0 {
  369. cluster.UserImpersonateGroups = strings.Join(kcAuthInfo.ImpersonateGroups, ",")
  370. }
  371. if len(kcCluster.CertificateAuthorityData) > 0 {
  372. cluster.CertificateAuthorityData = kcCluster.CertificateAuthorityData
  373. }
  374. if rcf.Resolver.ClusterCAData != "" {
  375. decoded, err := base64.StdEncoding.DecodeString(rcf.Resolver.ClusterCAData)
  376. // skip if decoding error
  377. if err != nil {
  378. return nil, err
  379. }
  380. cluster.CertificateAuthorityData = decoded
  381. }
  382. if rcf.Resolver.ClusterHostname != "" {
  383. serverURL, err := url.Parse(cluster.Server)
  384. if err != nil {
  385. return nil, err
  386. }
  387. if serverURL.Port() == "" {
  388. serverURL.Host = rcf.Resolver.ClusterHostname
  389. } else {
  390. serverURL.Host = rcf.Resolver.ClusterHostname + ":" + serverURL.Port()
  391. }
  392. cluster.Server = serverURL.String()
  393. }
  394. switch cc.AuthMechanism {
  395. case models.X509, models.Bearer, models.Basic, models.Local:
  396. cluster.KubeIntegrationID = rcf.IntegrationID
  397. case models.OIDC:
  398. cluster.OIDCIntegrationID = rcf.IntegrationID
  399. case models.GCP:
  400. cluster.GCPIntegrationID = rcf.IntegrationID
  401. case models.AWS:
  402. cluster.AWSIntegrationID = rcf.IntegrationID
  403. }
  404. return cluster, nil
  405. }