@@ -8,14 +8,25 @@ import (
88 "crypto/x509"
99 "encoding/pem"
1010
11+ "github.com/golang-jwt/jwt/v4"
12+
1113 "github.com/openshift/installer/pkg/asset"
14+ "github.com/openshift/installer/pkg/asset/agent/common"
1215)
1316
14- // AuthConfig is an asset that generates ECDSA public and private keys.
17+ // AuthConfig is an asset that generates ECDSA public/ private keys, JWT token .
1518type AuthConfig struct {
16- PublicKey , PrivateKey string
19+ PublicKey , PrivateKey , Token string
1720}
1821
22+ // LocalJWTKeyType suggests the key type to be used for the token.
23+ type LocalJWTKeyType string
24+
25+ const (
26+ // InfraEnvKey is used to generate token using infra env id.
27+ InfraEnvKey LocalJWTKeyType = "infra_env_id"
28+ )
29+
1930var _ asset.WritableAsset = (* AuthConfig )(nil )
2031
2132// Dependencies returns the assets on which the AuthConfig asset depends.
@@ -32,6 +43,12 @@ func (a *AuthConfig) Generate(dependencies asset.Parents) error {
3243 a .PublicKey = PublicKey
3344 a .PrivateKey = PrivateKey
3445
46+ token , err := localJWTForKey (common .InfraEnvID , a .PrivateKey )
47+ if err != nil {
48+ return err
49+ }
50+ a .Token = token
51+
3552 return nil
3653}
3754
@@ -53,7 +70,7 @@ func (a *AuthConfig) Files() []*asset.File {
5370 return []* asset.File {}
5471}
5572
56- // Reused from assisted-service.
73+ // Referenced from assisted-service.
5774// https://github.com/openshift/assisted-service/blob/d3c0122452c74ad208055b8b6ee412812431a83f/internal/gencrypto/keys.go#L13-L54
5875func keyPairPEM () (string , string , error ) {
5976 priv , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
@@ -97,3 +114,23 @@ func keyPairPEM() (string, string, error) {
97114
98115 return pubKeyPEM .String (), privKeyPEM .String (), nil
99116}
117+
118+ // Referenced from assisted-service.
119+ // https://github.com/openshift/assisted-service/blob/d3c0122452c74ad208055b8b6ee412812431a83f/internal/gencrypto/token.go#L33-L50
120+ func localJWTForKey (id string , privateKkeyPem string ) (string , error ) {
121+ priv , err := jwt .ParseECPrivateKeyFromPEM ([]byte (privateKkeyPem ))
122+ if err != nil {
123+ return "" , err
124+ }
125+
126+ token := jwt .NewWithClaims (jwt .SigningMethodES256 , jwt.MapClaims {
127+ string (InfraEnvKey ): id ,
128+ })
129+
130+ tokenString , err := token .SignedString (priv )
131+ if err != nil {
132+ return "" , err
133+ }
134+
135+ return tokenString , nil
136+ }
0 commit comments