@@ -8,30 +8,51 @@ 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
19- var _ asset.WritableAsset = (* AuthConfig )(nil )
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+
30+ var _ asset.Asset = (* AuthConfig )(nil )
2031
2132// Dependencies returns the assets on which the AuthConfig asset depends.
2233func (a * AuthConfig ) Dependencies () []asset.Asset {
23- return []asset.Asset {}
34+ return []asset.Asset {
35+ & common.InfraEnvID {},
36+ }
2437}
2538
2639// Generate generates the auth config for agent installer APIs.
2740func (a * AuthConfig ) Generate (dependencies asset.Parents ) error {
41+ infraEnvID := & common.InfraEnvID {}
42+ dependencies .Get (infraEnvID )
2843 PublicKey , PrivateKey , err := keyPairPEM ()
2944 if err != nil {
3045 return err
3146 }
3247 a .PublicKey = PublicKey
3348 a .PrivateKey = PrivateKey
3449
50+ token , err := localJWTForKey (infraEnvID .ID , a .PrivateKey )
51+ if err != nil {
52+ return err
53+ }
54+ a .Token = token
55+
3556 return nil
3657}
3758
@@ -40,21 +61,6 @@ func (a *AuthConfig) Name() string {
4061 return "Agent Installer API Auth Config"
4162}
4263
43- // Load returns the auth config from disk.
44- func (a * AuthConfig ) Load (f asset.FileFetcher ) (bool , error ) {
45- // The AuthConfig will not be needed by another asset so load is noop.
46- // This is implemented because it is required by WritableAsset
47- return false , nil
48- }
49-
50- // Files returns the files generated by the asset.
51- func (a * AuthConfig ) Files () []* asset.File {
52- // Return empty array because File will never be loaded.
53- return []* asset.File {}
54- }
55-
56- // Reused from assisted-service.
57- // https://github.com/openshift/assisted-service/blob/d3c0122452c74ad208055b8b6ee412812431a83f/internal/gencrypto/keys.go#L13-L54
5864func keyPairPEM () (string , string , error ) {
5965 priv , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
6066 if err != nil {
@@ -97,3 +103,21 @@ func keyPairPEM() (string, string, error) {
97103
98104 return pubKeyPEM .String (), privKeyPEM .String (), nil
99105}
106+
107+ func localJWTForKey (id string , privateKkeyPem string ) (string , error ) {
108+ priv , err := jwt .ParseECPrivateKeyFromPEM ([]byte (privateKkeyPem ))
109+ if err != nil {
110+ return "" , err
111+ }
112+
113+ token := jwt .NewWithClaims (jwt .SigningMethodES256 , jwt.MapClaims {
114+ string (InfraEnvKey ): id ,
115+ })
116+
117+ tokenString , err := token .SignedString (priv )
118+ if err != nil {
119+ return "" , err
120+ }
121+
122+ return tokenString , nil
123+ }
0 commit comments