@@ -17,7 +17,6 @@ package helpers
17
17
import (
18
18
"bytes"
19
19
"context"
20
- "crypto/rsa"
21
20
"crypto/tls"
22
21
"crypto/x509/pkix"
23
22
"encoding/json"
@@ -44,6 +43,7 @@ import (
44
43
"github.com/gravitational/teleport/api/breaker"
45
44
clientproto "github.com/gravitational/teleport/api/client/proto"
46
45
"github.com/gravitational/teleport/api/types"
46
+ "github.com/gravitational/teleport/api/utils/keys"
47
47
"github.com/gravitational/teleport/lib/auth/authclient"
48
48
"github.com/gravitational/teleport/lib/auth/keygen"
49
49
"github.com/gravitational/teleport/lib/auth/state"
@@ -97,11 +97,15 @@ type InstanceSecrets struct {
97
97
// PrivKey is instance private key
98
98
PrivKey []byte `json:"priv"`
99
99
// Cert is SSH host certificate
100
- Cert []byte `json:"cert"`
101
- // TLSCACert is the certificate of the trusted certificate authority
102
- TLSCACert []byte `json:"tls_ca_cert"`
103
- // TLSCert is client TLS X509 certificate
104
- TLSCert []byte `json:"tls_cert"`
100
+ SSHHostCert []byte `json:"cert"`
101
+ // TLSHostCACert is the certificate of the trusted host certificate authority
102
+ TLSHostCACert []byte `json:"tls_host_ca_cert"`
103
+ // TLSCert is client TLS host X509 certificate
104
+ TLSHostCert []byte `json:"tls_host_cert"`
105
+ // TLSUserCACert is the certificate of the trusted user certificate authority
106
+ TLSUserCACert []byte `json:"tls_user_ca_cert"`
107
+ // TLSUserCert is client TLS user X509 certificate
108
+ TLSUserCert []byte `json:"tls_user_cert"`
105
109
// TunnelAddr is a reverse tunnel listening port, allowing
106
110
// other sites to connect to i instance. Set to empty
107
111
// string if i instance is not allowing incoming tunnels
@@ -132,9 +136,7 @@ func (s *InstanceSecrets) GetRoles(t *testing.T) []types.Role {
132
136
return roles
133
137
}
134
138
135
- // GetCAs return an array of CAs stored by the secrets object. In i
136
- // case we always return hard-coded userCA + hostCA (and they share keys
137
- // for simplicity)
139
+ // GetCAs return an array of CAs stored by the secrets object
138
140
func (s * InstanceSecrets ) GetCAs () ([]types.CertAuthority , error ) {
139
141
hostCA , err := types .NewCertAuthority (types.CertAuthoritySpecV2 {
140
142
Type : types .HostCA ,
@@ -148,7 +150,7 @@ func (s *InstanceSecrets) GetCAs() ([]types.CertAuthority, error) {
148
150
TLS : []* types.TLSKeyPair {{
149
151
Key : s .PrivKey ,
150
152
KeyType : types .PrivateKeyType_RAW ,
151
- Cert : s .TLSCACert ,
153
+ Cert : s .TLSHostCACert ,
152
154
}},
153
155
},
154
156
})
@@ -168,7 +170,7 @@ func (s *InstanceSecrets) GetCAs() ([]types.CertAuthority, error) {
168
170
TLS : []* types.TLSKeyPair {{
169
171
Key : s .PrivKey ,
170
172
KeyType : types .PrivateKeyType_RAW ,
171
- Cert : s .TLSCACert ,
173
+ Cert : s .TLSUserCACert ,
172
174
}},
173
175
},
174
176
Roles : []string {services .RoleNameForCertAuthority (s .SiteName )},
@@ -184,7 +186,7 @@ func (s *InstanceSecrets) GetCAs() ([]types.CertAuthority, error) {
184
186
TLS : []* types.TLSKeyPair {{
185
187
Key : s .PrivKey ,
186
188
KeyType : types .PrivateKeyType_RAW ,
187
- Cert : s .TLSCACert ,
189
+ Cert : s .TLSHostCACert ,
188
190
}},
189
191
},
190
192
})
@@ -199,7 +201,7 @@ func (s *InstanceSecrets) GetCAs() ([]types.CertAuthority, error) {
199
201
TLS : []* types.TLSKeyPair {{
200
202
Key : s .PrivKey ,
201
203
KeyType : types .PrivateKeyType_RAW ,
202
- Cert : s .TLSCACert ,
204
+ Cert : s .TLSHostCACert ,
203
205
}},
204
206
},
205
207
})
@@ -256,9 +258,9 @@ func (s *InstanceSecrets) AsSlice() []*InstanceSecrets {
256
258
257
259
func (s * InstanceSecrets ) GetIdentity () * state.Identity {
258
260
i , err := state .ReadIdentityFromKeyPair (s .PrivKey , & clientproto.Certs {
259
- SSH : s .Cert ,
260
- TLS : s .TLSCert ,
261
- TLSCACerts : [][]byte {s .TLSCACert },
261
+ SSH : s .SSHHostCert ,
262
+ TLS : s .TLSHostCert ,
263
+ TLSCACerts : [][]byte {s .TLSHostCACert },
262
264
})
263
265
fatalIf (err )
264
266
return i
@@ -338,20 +340,14 @@ func NewInstance(t *testing.T, cfg InstanceConfig) *TeleInstance {
338
340
if cfg .Priv == nil || cfg .Pub == nil {
339
341
cfg .Priv , cfg .Pub , _ = keygen .GenerateKeyPair ()
340
342
}
341
- rsaKey , err := ssh . ParseRawPrivateKey (cfg .Priv )
343
+ key , err := keys . ParsePrivateKey (cfg .Priv )
342
344
fatalIf (err )
343
345
344
- tlsCACert , err := tlsca .GenerateSelfSignedCAWithSigner (rsaKey .(* rsa.PrivateKey ), pkix.Name {
345
- CommonName : cfg .ClusterName ,
346
- Organization : []string {cfg .ClusterName },
347
- }, nil , defaults .CATTL )
348
- fatalIf (err )
349
-
350
- signer , err := ssh .ParsePrivateKey (cfg .Priv )
346
+ sshSigner , err := ssh .NewSignerFromSigner (key )
351
347
fatalIf (err )
352
348
353
- cert , err := keygen .GenerateHostCert (services.HostCertParams {
354
- CASigner : signer ,
349
+ hostCert , err := keygen .GenerateHostCert (services.HostCertParams {
350
+ CASigner : sshSigner ,
355
351
PublicHostKey : cfg .Pub ,
356
352
HostID : cfg .HostID ,
357
353
NodeName : cfg .NodeName ,
@@ -360,23 +356,48 @@ func NewInstance(t *testing.T, cfg InstanceConfig) *TeleInstance {
360
356
TTL : 24 * time .Hour ,
361
357
})
362
358
fatalIf (err )
363
- tlsCA , err := tlsca .FromKeys (tlsCACert , cfg .Priv )
364
- fatalIf (err )
365
- cryptoPubKey , err := sshutils .CryptoPublicKey (cfg .Pub )
366
- fatalIf (err )
367
- identity := tlsca.Identity {
368
- Username : fmt .Sprintf ("%v.%v" , cfg .HostID , cfg .ClusterName ),
369
- Groups : []string {string (types .RoleAdmin )},
370
- }
359
+
371
360
clock := cfg .Clock
372
361
if clock == nil {
373
362
clock = clockwork .NewRealClock ()
374
363
}
364
+
365
+ identity := tlsca.Identity {
366
+ Username : fmt .Sprintf ("%v.%v" , cfg .HostID , cfg .ClusterName ),
367
+ Groups : []string {string (types .RoleAdmin )},
368
+ }
375
369
subject , err := identity .Subject ()
376
370
fatalIf (err )
377
- tlsCert , err := tlsCA .GenerateCertificate (tlsca.CertificateRequest {
371
+
372
+ tlsCAHostCert , err := tlsca .GenerateSelfSignedCAWithSigner (key , pkix.Name {
373
+ CommonName : cfg .ClusterName ,
374
+ Organization : []string {cfg .ClusterName },
375
+ }, nil , defaults .CATTL )
376
+ fatalIf (err )
377
+ tlsHostCA , err := tlsca .FromKeys (tlsCAHostCert , cfg .Priv )
378
+ fatalIf (err )
379
+ hostCryptoPubKey , err := sshutils .CryptoPublicKey (cfg .Pub )
380
+ fatalIf (err )
381
+ tlsHostCert , err := tlsHostCA .GenerateCertificate (tlsca.CertificateRequest {
382
+ Clock : clock ,
383
+ PublicKey : hostCryptoPubKey ,
384
+ Subject : subject ,
385
+ NotAfter : clock .Now ().UTC ().Add (time .Hour * 24 ),
386
+ })
387
+ fatalIf (err )
388
+
389
+ tlsCAUserCert , err := tlsca .GenerateSelfSignedCAWithSigner (key , pkix.Name {
390
+ CommonName : cfg .ClusterName ,
391
+ Organization : []string {cfg .ClusterName },
392
+ }, nil , defaults .CATTL )
393
+ fatalIf (err )
394
+ tlsUserCA , err := tlsca .FromKeys (tlsCAHostCert , cfg .Priv )
395
+ fatalIf (err )
396
+ userCryptoPubKey , err := sshutils .CryptoPublicKey (cfg .Pub )
397
+ fatalIf (err )
398
+ tlsUserCert , err := tlsUserCA .GenerateCertificate (tlsca.CertificateRequest {
378
399
Clock : clock ,
379
- PublicKey : cryptoPubKey ,
400
+ PublicKey : userCryptoPubKey ,
380
401
Subject : subject ,
381
402
NotAfter : clock .Now ().UTC ().Add (time .Hour * 24 ),
382
403
})
@@ -391,14 +412,16 @@ func NewInstance(t *testing.T, cfg InstanceConfig) *TeleInstance {
391
412
}
392
413
393
414
secrets := InstanceSecrets {
394
- SiteName : cfg .ClusterName ,
395
- PrivKey : cfg .Priv ,
396
- PubKey : cfg .Pub ,
397
- Cert : cert ,
398
- TLSCACert : tlsCACert ,
399
- TLSCert : tlsCert ,
400
- TunnelAddr : i .ReverseTunnel ,
401
- Users : make (map [string ]* User ),
415
+ SiteName : cfg .ClusterName ,
416
+ PrivKey : cfg .Priv ,
417
+ PubKey : cfg .Pub ,
418
+ SSHHostCert : hostCert ,
419
+ TLSHostCACert : tlsCAHostCert ,
420
+ TLSHostCert : tlsHostCert ,
421
+ TLSUserCACert : tlsCAUserCert ,
422
+ TLSUserCert : tlsUserCert ,
423
+ TunnelAddr : i .ReverseTunnel ,
424
+ Users : make (map [string ]* User ),
402
425
}
403
426
404
427
i .Secrets = secrets
0 commit comments