kube.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package integrations
  2. import "gorm.io/gorm"
  3. // KubeIntegrationName is the name of a kube auth mechanism
  4. type KubeIntegrationName string
  5. // The supported kube auth mechanisms
  6. const (
  7. KubeX509 KubeIntegrationName = "x509"
  8. KubeBasic = "basic"
  9. KubeBearer = "bearer"
  10. KubeLocal = "local"
  11. )
  12. // KubeIntegration represents the kube-native auth mechanisms: using x509 certs,
  13. // basic (username/password), bearer tokens, or local (using local kubeconfig)
  14. type KubeIntegration struct {
  15. gorm.Model
  16. // The name of the auth mechanism
  17. Mechanism KubeIntegrationName `json:"mechanism"`
  18. // The id of the user that linked this auth mechanism
  19. UserID uint `json:"user_id"`
  20. // The project that this integration belongs to
  21. ProjectID uint `json:"project_id"`
  22. // ------------------------------------------------------------------
  23. // All fields encrypted before storage.
  24. // ------------------------------------------------------------------
  25. // Certificate data is used by x509 auth mechanisms over TLS
  26. ClientCertificateData []byte `json:"client-certificate-data,omitempty"`
  27. ClientKeyData []byte `json:"client-key-data,omitempty"`
  28. // Token is used for bearer-token auth mechanisms
  29. Token []byte `json:"token,omitempty"`
  30. // Username/Password for basic authentication to a cluster
  31. Username []byte `json:"username,omitempty"`
  32. Password []byte `json:"password,omitempty"`
  33. // The raw kubeconfig, used by local auth mechanisms
  34. Kubeconfig []byte `json:"kubeconfig"`
  35. }
  36. // KubeIntegrationExternal is a KubeIntegration to be shared over REST
  37. type KubeIntegrationExternal struct {
  38. ID uint `json:"id"`
  39. // The name of the auth mechanism
  40. Mechanism KubeIntegrationName `json:"mechanism"`
  41. // The id of the user that linked this auth mechanism
  42. UserID uint `json:"user_id"`
  43. // The project that this integration belongs to
  44. ProjectID uint `json:"project_id"`
  45. }
  46. // Externalize generates an external KubeIntegration to be shared over REST
  47. func (k *KubeIntegration) Externalize() *KubeIntegrationExternal {
  48. return &KubeIntegrationExternal{
  49. ID: k.ID,
  50. Mechanism: k.Mechanism,
  51. UserID: k.UserID,
  52. ProjectID: k.ProjectID,
  53. }
  54. }
  55. // ToProjectIntegration converts a gcp integration to a project integration
  56. func (k *KubeIntegration) ToProjectIntegration(
  57. category string,
  58. service IntegrationService,
  59. ) *ProjectIntegration {
  60. return &ProjectIntegration{
  61. ID: k.ID,
  62. ProjectID: k.ProjectID,
  63. AuthMechanism: "kube",
  64. Category: category,
  65. Service: service,
  66. }
  67. }