|
| 1 | +package certificate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type ProviderType string |
| 11 | + |
| 12 | +const ( |
| 13 | + Auto ProviderType = "auto" |
| 14 | + CertManager ProviderType = "cert-manager" |
| 15 | + // add more ... |
| 16 | +) |
| 17 | + |
| 18 | +func NewProvider(pt ProviderType) (Provider, error) { |
| 19 | + switch pt { |
| 20 | + case Auto: |
| 21 | + return nil, nil // change me later |
| 22 | + case CertManager: |
| 23 | + return nil, nil // change me later |
| 24 | + } |
| 25 | + |
| 26 | + return nil, fmt.Errorf("unknown provider type: %s", pt) |
| 27 | +} |
| 28 | + |
| 29 | +// AltNames contains the domain names and IP addresses that will be added |
| 30 | +// to the x509 certificate SubAltNames fields. The values will be passed |
| 31 | +// directly to the x509.Certificate object. |
| 32 | +type AltNames struct { |
| 33 | + DNSNames []string |
| 34 | + IPs []net.IP |
| 35 | +} |
| 36 | + |
| 37 | +// Config contains the basic fields required for creating a certificate |
| 38 | +type Config struct { |
| 39 | + CommonName string |
| 40 | + Organization []string |
| 41 | + AltNames AltNames |
| 42 | + ValidityDuration time.Duration |
| 43 | + CABundle []byte |
| 44 | + |
| 45 | + // ExtraConfig contains provider specific configurations. |
| 46 | + ExtraConfig map[string]any |
| 47 | +} |
| 48 | + |
| 49 | +type Provider interface { |
| 50 | + // EnsureCertificateSecret ensures the specified certificate is |
| 51 | + // available as a Secret in Kubernetes. If the Secret does not |
| 52 | + // exist, it will be created. |
| 53 | + // |
| 54 | + // Parameters: |
| 55 | + // - ctx: Context for cancellation and deadlines. |
| 56 | + // - secretName: Name of the Secret to ensure. |
| 57 | + // - namespace: Namespace where the Secret should reside. |
| 58 | + // - cfg: Configuration for the certificate. |
| 59 | + // |
| 60 | + // Returns: |
| 61 | + // - nil if the operation succeeds, or an error otherwise. |
| 62 | + EnsureCertificateSecret(ctx context.Context, secretName string, namespace string, cfg *Config) error |
| 63 | + |
| 64 | + // ValidateCertificateSecret validates the certificate stored |
| 65 | + // in the specified Secret. This checks if the certificate is |
| 66 | + // valid (e.g., not expired, matches configuration). |
| 67 | + // |
| 68 | + // Parameters: |
| 69 | + // - ctx: Context for cancellation and deadlines. |
| 70 | + // - secretName: Name of the Secret to validate. |
| 71 | + // - namespace: Namespace where the Secret resides. |
| 72 | + // - cfg: Configuration to validate against. |
| 73 | + // |
| 74 | + // Returns: |
| 75 | + // - true if the Secret is valid, false otherwise, along with |
| 76 | + // an error if validation fails. |
| 77 | + ValidateCertificateSecret(ctx context.Context, secretName string, namespace string, cfg *Config) (bool, error) |
| 78 | + |
| 79 | + // DeleteCertificateSecret explicitly deletes the Secret containing |
| 80 | + // the certificate. This should only be used if the certificate |
| 81 | + // is no longer needed. |
| 82 | + // |
| 83 | + // Parameters: |
| 84 | + // - ctx: Context for cancellation and deadlines. |
| 85 | + // - secretName: Name of the Secret to delete. |
| 86 | + // - namespace: Namespace where the Secret resides. |
| 87 | + // |
| 88 | + // Returns: |
| 89 | + // - nil if the operation succeeds, or an error otherwise. |
| 90 | + DeleteCertificateSecret(ctx context.Context, secretName string, namespace string) error |
| 91 | + |
| 92 | + // RevokeCertificate revokes a certificate if supported by the provider. |
| 93 | + // |
| 94 | + // Parameters: |
| 95 | + // - ctx: Context for cancellation and deadlines. |
| 96 | + // - secretName: Name of the Secret containing the certificate to revoke. |
| 97 | + // - namespace: Namespace where the Secret resides. |
| 98 | + // |
| 99 | + // Returns: |
| 100 | + // - nil if the revocation succeeds, or an error otherwise. |
| 101 | + RevokeCertificate(ctx context.Context, secretName string, namespace string) error |
| 102 | + |
| 103 | + // GetCertificateConfig returns the certificate configuration from the provider. |
| 104 | + // |
| 105 | + // Parameters: |
| 106 | + // - secretName: Name of the Secret containing the certificate. |
| 107 | + // - namespace: Namespace where the Secret resides. |
| 108 | + // |
| 109 | + // Returns: |
| 110 | + // - Config if the Secret exists and is valid, or an error otherwise. |
| 111 | + GetCertificateConfig(ctx context.Context, secretName string, namespace string) (*Config, error) |
| 112 | +} |
0 commit comments