cluster.go 13 KB

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