@@ -90,11 +90,37 @@ func NewSelfSignedCACert(cfg Config, key crypto.Signer) (*x509.Certificate, erro
90
90
return x509 .ParseCertificate (certDERBytes )
91
91
}
92
92
93
+ // SelfSignedCertKeyOptions contains configuration parameters for generating self-signed certificates.
94
+ type SelfSignedCertKeyOptions struct {
95
+ // Host is required, and identifies the host of the serving certificate. Can be a DNS name or IP address.
96
+ Host string
97
+ // AlternateIPs is optional, and identifies additional IPs the serving certificate should be valid for.
98
+ AlternateIPs []net.IP
99
+ // AlternateDNS is optional, and identifies additional DNS names the serving certificate should be valid for.
100
+ AlternateDNS []string
101
+
102
+ // MaxAge controls the duration of the issued certificate.
103
+ // Defaults to 1 year if unset.
104
+ // Ignored if FixtureDirectory is set.
105
+ MaxAge time.Duration
106
+
107
+ // FixtureDirectory is intended for use in tests.
108
+ // If non-empty, it is a directory path which can contain pre-generated certs. The format is:
109
+ // <host>_<ip>-<ip>_<alternateDNS>-<alternateDNS>.crt
110
+ // <host>_<ip>-<ip>_<alternateDNS>-<alternateDNS>.key
111
+ // Certs/keys not existing in that directory are created with a duration of 100 years.
112
+ FixtureDirectory string
113
+ }
114
+
93
115
// GenerateSelfSignedCertKey creates a self-signed certificate and key for the given host.
94
116
// Host may be an IP or a DNS name
95
117
// You may also specify additional subject alt names (either ip or dns names) for the certificate.
96
118
func GenerateSelfSignedCertKey (host string , alternateIPs []net.IP , alternateDNS []string ) ([]byte , []byte , error ) {
97
- return GenerateSelfSignedCertKeyWithFixtures (host , alternateIPs , alternateDNS , "" )
119
+ return GenerateSelfSignedCertKeyWithOptions (SelfSignedCertKeyOptions {
120
+ Host : host ,
121
+ AlternateIPs : alternateIPs ,
122
+ AlternateDNS : alternateDNS ,
123
+ })
98
124
}
99
125
100
126
// GenerateSelfSignedCertKeyWithFixtures creates a self-signed certificate and key for the given host.
@@ -106,8 +132,26 @@ func GenerateSelfSignedCertKey(host string, alternateIPs []net.IP, alternateDNS
106
132
// <host>_<ip>-<ip>_<alternateDNS>-<alternateDNS>.key
107
133
// Certs/keys not existing in that directory are created.
108
134
func GenerateSelfSignedCertKeyWithFixtures (host string , alternateIPs []net.IP , alternateDNS []string , fixtureDirectory string ) ([]byte , []byte , error ) {
135
+ return GenerateSelfSignedCertKeyWithOptions (SelfSignedCertKeyOptions {
136
+ Host : host ,
137
+ AlternateIPs : alternateIPs ,
138
+ AlternateDNS : alternateDNS ,
139
+ FixtureDirectory : fixtureDirectory ,
140
+ })
141
+ }
142
+
143
+ // GenerateSelfSignedCertKeyWithOptions generates a self-signed certificate and key based on the provided options.
144
+ func GenerateSelfSignedCertKeyWithOptions (opts SelfSignedCertKeyOptions ) ([]byte , []byte , error ) {
145
+ host := opts .Host
146
+ alternateIPs := opts .AlternateIPs
147
+ alternateDNS := opts .AlternateDNS
148
+ fixtureDirectory := opts .FixtureDirectory
149
+ maxAge := opts .MaxAge
150
+ if maxAge == 0 {
151
+ maxAge = 365 * 24 * time .Hour
152
+ }
153
+
109
154
validFrom := time .Now ().Add (- time .Hour ) // valid an hour earlier to avoid flakes due to clock skew
110
- maxAge := time .Hour * 24 * 365 // one year self-signed certs
111
155
112
156
baseName := fmt .Sprintf ("%s_%s_%s" , host , strings .Join (ipsToStrings (alternateIPs ), "-" ), strings .Join (alternateDNS , "-" ))
113
157
certFixturePath := filepath .Join (fixtureDirectory , baseName + ".crt" )
0 commit comments