Skip to content

Commit 54fb3a1

Browse files
Merge pull request openshift#8270 from pawanpinjarkar/authenticate-wait-for
AGENT-871: Authenticate wait for
2 parents d000034 + e47dc98 commit 54fb3a1

File tree

9 files changed

+63
-21
lines changed

9 files changed

+63
-21
lines changed

data/data/agent/files/usr/local/share/assisted-service/assisted-service.env.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ OPENSHIFT_INSTALL_RELEASE_IMAGE_MIRROR={{.ReleaseImageMirror}}
1919
STORAGE=filesystem
2020
INFRA_ENV_ID={{.InfraEnvID}}
2121
EC_PUBLIC_KEY_PEM={{.PublicKeyPEM}}
22-
EC_PRIVATE_KEY_PEM={{.PrivateKeyPEM}}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ require (
4242
github.com/diskfs/go-diskfs v1.4.0
4343
github.com/form3tech-oss/jwt-go v3.2.3+incompatible
4444
github.com/go-openapi/errors v0.22.0
45+
github.com/go-openapi/runtime v0.28.0
4546
github.com/go-openapi/strfmt v0.23.0
4647
github.com/go-openapi/swag v0.23.0
4748
github.com/go-playground/validator/v10 v10.19.0
@@ -177,7 +178,6 @@ require (
177178
github.com/go-openapi/jsonpointer v0.21.0 // indirect
178179
github.com/go-openapi/jsonreference v0.21.0 // indirect
179180
github.com/go-openapi/loads v0.22.0 // indirect
180-
github.com/go-openapi/runtime v0.28.0 // indirect
181181
github.com/go-openapi/spec v0.21.0 // indirect
182182
github.com/go-openapi/validate v0.24.0 // indirect
183183
github.com/go-playground/locales v0.14.1 // indirect

pkg/agent/cluster.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ func NewCluster(ctx context.Context, assetDir, rendezvousIP, kubeconfigPath, ssh
7070
czero := &Cluster{}
7171
capi := &clientSet{}
7272

73-
restclient, err := NewNodeZeroRestClient(ctx, rendezvousIP, sshKey)
73+
authToken, err := FindAuthTokenFromAssetStore(assetDir)
74+
if err != nil {
75+
logrus.Fatal(err)
76+
}
77+
78+
restclient, err := NewNodeZeroRestClient(ctx, rendezvousIP, sshKey, authToken)
7479
if err != nil {
7580
logrus.Fatal(err)
7681
}

pkg/agent/rest.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/openshift/assisted-service/client/installer"
1616
"github.com/openshift/assisted-service/models"
1717
"github.com/openshift/installer/pkg/asset/agent/agentconfig"
18+
"github.com/openshift/installer/pkg/asset/agent/gencrypto"
1819
"github.com/openshift/installer/pkg/asset/agent/image"
1920
"github.com/openshift/installer/pkg/asset/agent/manifests"
2021
"github.com/openshift/installer/pkg/asset/installconfig"
@@ -32,7 +33,7 @@ type NodeZeroRestClient struct {
3233
}
3334

3435
// NewNodeZeroRestClient Initialize a new rest client to interact with the Agent Rest API on node zero.
35-
func NewNodeZeroRestClient(ctx context.Context, rendezvousIP string, sshKey string) (*NodeZeroRestClient, error) {
36+
func NewNodeZeroRestClient(ctx context.Context, rendezvousIP, sshKey, token string) (*NodeZeroRestClient, error) {
3637
restClient := &NodeZeroRestClient{}
3738

3839
// Get SSH Keys which can be used to determine if Rest API failures are due to network connectivity issues
@@ -46,6 +47,9 @@ func NewNodeZeroRestClient(ctx context.Context, rendezvousIP string, sshKey stri
4647
Host: net.JoinHostPort(rendezvousIP, "8090"),
4748
Path: client.DefaultBasePath,
4849
}
50+
51+
config.AuthInfo = gencrypto.UserAuthHeaderWriter(token)
52+
4953
client := client.New(config)
5054

5155
restClient.Client = client
@@ -115,6 +119,27 @@ func FindRendezvouIPAndSSHKeyFromAssetStore(assetDir string) (string, string, er
115119
return rendezvousIP, sshKey, nil
116120
}
117121

122+
// FindAuthTokenFromAssetStore returns the auth token.
123+
func FindAuthTokenFromAssetStore(assetDir string) (string, error) {
124+
authConfigAsset := &gencrypto.AuthConfig{}
125+
126+
assetStore, err := assetstore.NewStore(assetDir)
127+
if err != nil {
128+
return "", errors.Wrap(err, "failed to create asset store")
129+
}
130+
131+
authConfig, authConfigError := assetStore.Load(authConfigAsset)
132+
133+
if authConfigError != nil {
134+
logrus.Debug(errors.Wrapf(authConfigError, "failed to load %s", authConfigAsset.Name()))
135+
return "", errors.New("failed to load AuthConfig")
136+
}
137+
138+
token := authConfig.(*gencrypto.AuthConfig).Token
139+
140+
return token, nil
141+
}
142+
118143
// IsRestAPILive Determine if the Agent Rest API on node zero has initialized
119144
func (rest *NodeZeroRestClient) IsRestAPILive() bool {
120145
// GET /v2/infraenvs
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package gencrypto
2+
3+
import (
4+
"github.com/go-openapi/runtime"
5+
"github.com/go-openapi/strfmt"
6+
)
7+
8+
// UserAuthHeaderWriter sets the JWT authorization token.
9+
func UserAuthHeaderWriter(token string) runtime.ClientAuthInfoWriter {
10+
return runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
11+
return r.SetHeaderParam("Authorization", token)
12+
})
13+
}

pkg/asset/agent/gencrypto/authconfig.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/elliptic"
88
"crypto/rand"
99
"crypto/x509"
10+
"encoding/base64"
1011
"encoding/pem"
1112

1213
"github.com/golang-jwt/jwt/v4"
@@ -17,9 +18,11 @@ import (
1718

1819
// AuthConfig is an asset that generates ECDSA public/private keys, JWT token.
1920
type AuthConfig struct {
20-
PublicKey, PrivateKey, Token string
21+
PublicKey, Token string
2122
}
2223

24+
var _ asset.Asset = (*AuthConfig)(nil)
25+
2326
// LocalJWTKeyType suggests the key type to be used for the token.
2427
type LocalJWTKeyType string
2528

@@ -41,14 +44,17 @@ func (a *AuthConfig) Dependencies() []asset.Asset {
4144
func (a *AuthConfig) Generate(_ context.Context, dependencies asset.Parents) error {
4245
infraEnvID := &common.InfraEnvID{}
4346
dependencies.Get(infraEnvID)
44-
PublicKey, PrivateKey, err := keyPairPEM()
47+
48+
publicKey, privateKey, err := keyPairPEM()
4549
if err != nil {
4650
return err
4751
}
48-
a.PublicKey = PublicKey
49-
a.PrivateKey = PrivateKey
52+
// Encode to Base64 (Standard encoding)
53+
encodedPubKeyPEM := base64.StdEncoding.EncodeToString([]byte(publicKey))
54+
55+
a.PublicKey = encodedPubKeyPEM
5056

51-
token, err := localJWTForKey(infraEnvID.ID, a.PrivateKey)
57+
token, err := localJWTForKey(infraEnvID.ID, privateKey)
5258
if err != nil {
5359
return err
5460
}
@@ -58,7 +64,7 @@ func (a *AuthConfig) Generate(_ context.Context, dependencies asset.Parents) err
5864
}
5965

6066
// Name returns the human-friendly name of the asset.
61-
func (a *AuthConfig) Name() string {
67+
func (*AuthConfig) Name() string {
6268
return "Agent Installer API Auth Config"
6369
}
6470

pkg/asset/agent/gencrypto/authconfig_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestAuthConfig_Generate(t *testing.T) {
1515
name string
1616
}{
1717
{
18-
name: "generate-public-private-keys",
18+
name: "generate-public-key-and-token",
1919
},
2020
}
2121
for _, tc := range cases {
@@ -28,8 +28,7 @@ func TestAuthConfig_Generate(t *testing.T) {
2828

2929
assert.NoError(t, err)
3030

31-
assert.Contains(t, authConfigAsset.PrivateKey, "BEGIN EC PRIVATE KEY")
32-
assert.Contains(t, authConfigAsset.PublicKey, "BEGIN EC PUBLIC KEY")
31+
assert.NotEmpty(t, authConfigAsset.PublicKey)
3332
assert.NotEmpty(t, authConfigAsset.Token)
3433
})
3534
}

pkg/asset/agent/image/ignition.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ type agentTemplateData struct {
7373
ConfigImageFiles string
7474
ImageTypeISO string
7575
PublicKeyPEM string
76-
PrivateKeyPEM string
7776
CaBundleMount string
7877
}
7978

@@ -255,7 +254,6 @@ func (a *Ignition) Generate(_ context.Context, dependencies asset.Parents) error
255254
osImage,
256255
infraEnv.Spec.Proxy,
257256
imageTypeISO,
258-
keyPairAsset.PrivateKey,
259257
keyPairAsset.PublicKey,
260258
caBundleMount)
261259

@@ -373,7 +371,7 @@ func getTemplateData(name, pullSecret, releaseImageList, releaseImage,
373371
osImage *models.OsImage,
374372
proxy *v1beta1.Proxy,
375373
imageTypeISO,
376-
privateKey, publicKey string,
374+
publicKey string,
377375
caBundleMount string) *agentTemplateData {
378376
return &agentTemplateData{
379377
ServiceProtocol: "http",
@@ -390,7 +388,6 @@ func getTemplateData(name, pullSecret, releaseImageList, releaseImage,
390388
OSImage: osImage,
391389
Proxy: proxy,
392390
ImageTypeISO: imageTypeISO,
393-
PrivateKeyPEM: privateKey,
394391
PublicKeyPEM: publicKey,
395392
CaBundleMount: caBundleMount,
396393
}

pkg/asset/agent/image/ignition_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ func TestIgnition_getTemplateData(t *testing.T) {
9191
}
9292
clusterName := "test-agent-cluster-install.test"
9393

94-
privateKey := "-----BEGIN EC PUBLIC KEY-----\nMFkwEwYHKoAiDHV4tg==\n-----END EC PUBLIC KEY-----\n"
95-
publicKey := "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIOSCfDNmx0qe6dncV4tg==\n-----END EC PRIVATE KEY-----\n" //nolint:gosec
94+
publicKey := "-----BEGIN EC PUBLIC KEY-----\nMHcCAQEEIOSCfDNmx0qe6dncV4tg==\n-----END EC PUBLIC KEY-----\n"
9695

97-
templateData := getTemplateData(clusterName, pullSecret, releaseImageList, releaseImage, releaseImageMirror, haveMirrorConfig, publicContainerRegistries, agentClusterInstall.Spec.ProvisionRequirements.ControlPlaneAgents, agentClusterInstall.Spec.ProvisionRequirements.WorkerAgents, infraEnvID, osImage, proxy, "minimal-iso", privateKey, publicKey, "")
96+
templateData := getTemplateData(clusterName, pullSecret, releaseImageList, releaseImage, releaseImageMirror, haveMirrorConfig, publicContainerRegistries, agentClusterInstall.Spec.ProvisionRequirements.ControlPlaneAgents, agentClusterInstall.Spec.ProvisionRequirements.WorkerAgents, infraEnvID, osImage, proxy, "minimal-iso", publicKey, "")
9897
assert.Equal(t, clusterName, templateData.ClusterName)
9998
assert.Equal(t, "http", templateData.ServiceProtocol)
10099
assert.Equal(t, pullSecret, templateData.PullSecret)
@@ -108,7 +107,6 @@ func TestIgnition_getTemplateData(t *testing.T) {
108107
assert.Equal(t, infraEnvID, templateData.InfraEnvID)
109108
assert.Equal(t, osImage, templateData.OSImage)
110109
assert.Equal(t, proxy, templateData.Proxy)
111-
assert.Equal(t, privateKey, templateData.PrivateKeyPEM)
112110
assert.Equal(t, publicKey, templateData.PublicKeyPEM)
113111
}
114112

0 commit comments

Comments
 (0)