security.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2020 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6.     http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v1
  14. import (
  15. "strings"
  16. "github.com/hashicorp/vault/api"
  17. "github.com/libopenstorage/secrets"
  18. "github.com/libopenstorage/secrets/vault"
  19. )
  20. var (
  21. VaultTLSConnectionDetails = []string{api.EnvVaultCACert, api.EnvVaultClientCert, api.EnvVaultClientKey}
  22. )
  23. // IsEnabled return whether a KMS is configured
  24. func (kms *KeyManagementServiceSpec) IsEnabled() bool {
  25. return len(kms.ConnectionDetails) != 0
  26. }
  27. // IsTokenAuthEnabled return whether KMS token auth is enabled
  28. func (kms *KeyManagementServiceSpec) IsTokenAuthEnabled() bool {
  29. return kms.TokenSecretName != ""
  30. }
  31. // IsK8sAuthEnabled return whether KMS Kubernetes auth is enabled
  32. func (kms *KeyManagementServiceSpec) IsK8sAuthEnabled() bool {
  33. return getParam(kms.ConnectionDetails, vault.AuthMethod) == vault.AuthMethodKubernetes && kms.TokenSecretName == ""
  34. }
  35. // IsVaultKMS return whether Vault KMS is configured
  36. func (kms *KeyManagementServiceSpec) IsVaultKMS() bool {
  37. return getParam(kms.ConnectionDetails, "KMS_PROVIDER") == secrets.TypeVault
  38. }
  39. // IsIBMKeyProtectKMS return whether IBM Key Protect KMS is configured
  40. func (kms *KeyManagementServiceSpec) IsIBMKeyProtectKMS() bool {
  41. return getParam(kms.ConnectionDetails, "KMS_PROVIDER") == "ibmkeyprotect"
  42. }
  43. // IsKMIPKMS return whether KMIP KMS is configured
  44. func (kms *KeyManagementServiceSpec) IsKMIPKMS() bool {
  45. return getParam(kms.ConnectionDetails, "KMS_PROVIDER") == "kmip"
  46. }
  47. // IsTLSEnabled return KMS TLS details are configured
  48. func (kms *KeyManagementServiceSpec) IsTLSEnabled() bool {
  49. for _, tlsOption := range VaultTLSConnectionDetails {
  50. tlsSecretName := getParam(kms.ConnectionDetails, tlsOption)
  51. if tlsSecretName != "" {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. // getParam returns the value of the KMS config option
  58. func getParam(kmsConfig map[string]string, param string) string {
  59. if val, ok := kmsConfig[param]; ok && val != "" {
  60. return strings.TrimSpace(val)
  61. }
  62. return ""
  63. }