|
| 1 | +package identity |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/ecdsa" |
| 6 | + "crypto/x509" |
| 7 | + "encoding/base64" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + hlfv1alpha1 "github.com/kfsoftware/hlf-operator/pkg/apis/hlf.kungfusoftware.es/v1alpha1" |
| 11 | + "github.com/kfsoftware/hlf-operator/pkg/pki" |
| 12 | + "github.com/pkg/errors" |
| 13 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/client-go/kubernetes" |
| 15 | +) |
| 16 | + |
| 17 | +// pkiHelper provides PKI operations for the identity controller using the unified PKI interface |
| 18 | +type pkiHelper struct { |
| 19 | + clientSet kubernetes.Interface |
| 20 | +} |
| 21 | + |
| 22 | +// newPKIHelper creates a new PKI helper |
| 23 | +func newPKIHelper(clientSet kubernetes.Interface) *pkiHelper { |
| 24 | + return &pkiHelper{clientSet: clientSet} |
| 25 | +} |
| 26 | + |
| 27 | +// createProvider creates the appropriate PKI provider based on credential store configuration |
| 28 | +func (h *pkiHelper) createProvider(ctx context.Context, conf *hlfv1alpha1.FabricIdentity) (pki.Provider, error) { |
| 29 | + switch conf.Spec.CredentialStore { |
| 30 | + case hlfv1alpha1.CredentialStoreVault: |
| 31 | + return h.createVaultProvider(ctx, conf) |
| 32 | + default: |
| 33 | + return h.createFabricCAProvider(ctx, conf) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// createFabricCAProvider creates a FabricCA PKI provider from identity config |
| 38 | +func (h *pkiHelper) createFabricCAProvider(ctx context.Context, conf *hlfv1alpha1.FabricIdentity) (pki.Provider, error) { |
| 39 | + cacert, err := h.getCertBytesFromCATLS(conf.Spec.Catls) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + caURL := fmt.Sprintf("https://%s:%d", conf.Spec.Cahost, conf.Spec.Caport) |
| 45 | + |
| 46 | + return pki.NewProvider(&pki.ProviderConfig{ |
| 47 | + Type: pki.ProviderTypeFabricCA, |
| 48 | + ClientSet: h.clientSet, |
| 49 | + FabricCA: &pki.FabricCAConfig{ |
| 50 | + URL: caURL, |
| 51 | + CAName: conf.Spec.Caname, |
| 52 | + TLSCert: string(cacert), |
| 53 | + MSPID: conf.Spec.MSPID, |
| 54 | + }, |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +// createVaultProvider creates a Vault PKI provider from identity config |
| 59 | +func (h *pkiHelper) createVaultProvider(ctx context.Context, conf *hlfv1alpha1.FabricIdentity) (pki.Provider, error) { |
| 60 | + if conf.Spec.Vault == nil { |
| 61 | + return nil, errors.New("vault configuration is required for vault credential store") |
| 62 | + } |
| 63 | + |
| 64 | + vaultConf := &conf.Spec.Vault.Vault |
| 65 | + vaultReq := &conf.Spec.Vault.Request |
| 66 | + |
| 67 | + return pki.NewProvider(&pki.ProviderConfig{ |
| 68 | + Type: pki.ProviderTypeVault, |
| 69 | + ClientSet: h.clientSet, |
| 70 | + Vault: &pki.VaultConfig{ |
| 71 | + URL: vaultConf.URL, |
| 72 | + PKIPath: vaultReq.PKI, |
| 73 | + Role: vaultReq.Role, |
| 74 | + TTL: vaultReq.TTL, |
| 75 | + Auth: pki.VaultAuthConfig{ |
| 76 | + TokenSecretRef: convertSecretRef(vaultConf.TokenSecretRef), |
| 77 | + }, |
| 78 | + TLS: pki.VaultTLSConfig{ |
| 79 | + CACert: vaultConf.CACert, |
| 80 | + ClientCert: vaultConf.ClientCert, |
| 81 | + ClientKeySecretRef: convertSecretRef(vaultConf.ClientKeySecretRef), |
| 82 | + ServerName: vaultConf.ServerName, |
| 83 | + SkipVerify: vaultConf.TLSSkipVerify, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +// CreateSignCryptoMaterialV2 creates sign crypto material using the PKI interface |
| 90 | +func (h *pkiHelper) CreateSignCryptoMaterialV2( |
| 91 | + ctx context.Context, |
| 92 | + conf *hlfv1alpha1.FabricIdentity, |
| 93 | +) (*x509.Certificate, *ecdsa.PrivateKey, *x509.Certificate, error) { |
| 94 | + provider, err := h.createProvider(ctx, conf) |
| 95 | + if err != nil { |
| 96 | + return nil, nil, nil, errors.Wrap(err, "failed to create PKI provider") |
| 97 | + } |
| 98 | + |
| 99 | + resp, err := provider.Enroll(ctx, pki.EnrollRequest{ |
| 100 | + User: conf.Spec.Enrollid, |
| 101 | + Secret: conf.Spec.Enrollsecret, |
| 102 | + CN: conf.Name, |
| 103 | + Hosts: []string{}, |
| 104 | + MSPID: conf.Spec.MSPID, |
| 105 | + Profile: "", |
| 106 | + }) |
| 107 | + if err != nil { |
| 108 | + return nil, nil, nil, errors.Wrap(err, "failed to enroll identity") |
| 109 | + } |
| 110 | + |
| 111 | + return resp.Certificate, resp.PrivateKey, resp.RootCertificate, nil |
| 112 | +} |
| 113 | + |
| 114 | +// ReenrollSignCryptoMaterialV2 re-enrolls sign crypto material using the PKI interface |
| 115 | +func (h *pkiHelper) ReenrollSignCryptoMaterialV2( |
| 116 | + ctx context.Context, |
| 117 | + conf *hlfv1alpha1.FabricIdentity, |
| 118 | + existingCert string, |
| 119 | + existingKey *ecdsa.PrivateKey, |
| 120 | +) (*x509.Certificate, *ecdsa.PrivateKey, *x509.Certificate, error) { |
| 121 | + provider, err := h.createProvider(ctx, conf) |
| 122 | + if err != nil { |
| 123 | + return nil, nil, nil, errors.Wrap(err, "failed to create PKI provider") |
| 124 | + } |
| 125 | + |
| 126 | + resp, err := provider.Reenroll(ctx, pki.ReenrollRequest{ |
| 127 | + EnrollID: conf.Spec.Enrollid, |
| 128 | + CN: conf.Name, |
| 129 | + Hosts: []string{}, |
| 130 | + MSPID: conf.Spec.MSPID, |
| 131 | + Profile: "", |
| 132 | + ExistingCert: existingCert, |
| 133 | + ExistingKey: existingKey, |
| 134 | + }) |
| 135 | + if err != nil { |
| 136 | + return nil, nil, nil, errors.Wrap(err, "failed to re-enroll identity") |
| 137 | + } |
| 138 | + |
| 139 | + return resp.Certificate, existingKey, resp.RootCertificate, nil |
| 140 | +} |
| 141 | + |
| 142 | +// RegisterUserV2 registers a user using the PKI interface |
| 143 | +func (h *pkiHelper) RegisterUserV2( |
| 144 | + ctx context.Context, |
| 145 | + conf *hlfv1alpha1.FabricIdentity, |
| 146 | + attributes []pki.Attribute, |
| 147 | +) (string, error) { |
| 148 | + provider, err := h.createProvider(ctx, conf) |
| 149 | + if err != nil { |
| 150 | + return "", errors.Wrap(err, "failed to create PKI provider") |
| 151 | + } |
| 152 | + |
| 153 | + // Check if provider supports registration |
| 154 | + if supporter, ok := provider.(pki.RegistrationSupporter); ok { |
| 155 | + if !supporter.SupportsRegistration() { |
| 156 | + return "", errors.Errorf("provider %s does not support identity registration", provider.Type()) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + resp, err := provider.Register(ctx, pki.RegisterRequest{ |
| 161 | + EnrollID: conf.Spec.Register.Enrollid, |
| 162 | + EnrollSecret: conf.Spec.Register.Enrollsecret, |
| 163 | + User: conf.Spec.Enrollid, |
| 164 | + Secret: conf.Spec.Enrollsecret, |
| 165 | + Type: conf.Spec.Register.Type, |
| 166 | + MSPID: conf.Spec.MSPID, |
| 167 | + Attributes: attributes, |
| 168 | + }) |
| 169 | + if err != nil { |
| 170 | + return "", errors.Wrap(err, "failed to register identity") |
| 171 | + } |
| 172 | + |
| 173 | + return resp.Secret, nil |
| 174 | +} |
| 175 | + |
| 176 | +// getCertBytesFromCATLS retrieves the CA TLS certificate bytes |
| 177 | +func (h *pkiHelper) getCertBytesFromCATLS(caTls *hlfv1alpha1.Catls) ([]byte, error) { |
| 178 | + var certBytes []byte |
| 179 | + var err error |
| 180 | + |
| 181 | + if caTls.Cacert != "" { |
| 182 | + certBytes, err = base64.StdEncoding.DecodeString(caTls.Cacert) |
| 183 | + if err != nil { |
| 184 | + return nil, errors.Wrap(err, "failed to decode CA cert from base64") |
| 185 | + } |
| 186 | + } else if caTls.SecretRef != nil { |
| 187 | + secret, err := h.clientSet.CoreV1().Secrets(caTls.SecretRef.Namespace).Get( |
| 188 | + context.Background(), |
| 189 | + caTls.SecretRef.Name, |
| 190 | + v1.GetOptions{}, |
| 191 | + ) |
| 192 | + if err != nil { |
| 193 | + return nil, errors.Wrap(err, "failed to get CA cert secret") |
| 194 | + } |
| 195 | + certBytes = secret.Data[caTls.SecretRef.Key] |
| 196 | + } else { |
| 197 | + return nil, errors.New("invalid CA TLS configuration: neither cacert nor secretRef provided") |
| 198 | + } |
| 199 | + |
| 200 | + return certBytes, nil |
| 201 | +} |
| 202 | + |
| 203 | +// convertSecretRef converts from hlfv1alpha1.VaultSecretRef to pki.SecretRef |
| 204 | +func convertSecretRef(ref *hlfv1alpha1.VaultSecretRef) *pki.SecretRef { |
| 205 | + if ref == nil { |
| 206 | + return nil |
| 207 | + } |
| 208 | + return &pki.SecretRef{ |
| 209 | + Namespace: ref.Namespace, |
| 210 | + Name: ref.Name, |
| 211 | + Key: ref.Key, |
| 212 | + } |
| 213 | +} |
0 commit comments