Skip to content

Commit 8560f7e

Browse files
committed
use new join service for host joins
1 parent c963a23 commit 8560f7e

File tree

10 files changed

+193
-116
lines changed

10 files changed

+193
-116
lines changed

integration/proxy/proxy_helpers.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ import (
5252
"github.com/gravitational/teleport/api/utils/retryutils"
5353
"github.com/gravitational/teleport/integration/helpers"
5454
"github.com/gravitational/teleport/lib"
55-
"github.com/gravitational/teleport/lib/auth/join"
5655
"github.com/gravitational/teleport/lib/auth/state"
5756
"github.com/gravitational/teleport/lib/client"
5857
"github.com/gravitational/teleport/lib/defaults"
58+
"github.com/gravitational/teleport/lib/join/joinclient"
5959
"github.com/gravitational/teleport/lib/kube/kubeconfig"
6060
testingkubemock "github.com/gravitational/teleport/lib/kube/proxy/testing/kube_server"
6161
"github.com/gravitational/teleport/lib/reversetunnelclient"
@@ -653,11 +653,10 @@ func mustRegisterUsingIAMMethod(t *testing.T, proxyAddr utils.NetAddr, token str
653653
t.Setenv("AWS_REGION", "us-west-2")
654654

655655
node := uuid.NewString()
656-
_, err = join.Register(context.TODO(), join.RegisterParams{
656+
_, err = joinclient.Join(t.Context(), joinclient.JoinParams{
657657
Token: token,
658658
ID: state.IdentityID{
659-
Role: types.RoleNode,
660-
HostUUID: node,
659+
Role: types.RoleInstance,
661660
NodeName: node,
662661
},
663662
ProxyServer: proxyAddr,

lib/auth/bot_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ import (
5757
"github.com/gravitational/teleport/lib/auth"
5858
"github.com/gravitational/teleport/lib/auth/authclient"
5959
"github.com/gravitational/teleport/lib/auth/authtest"
60-
"github.com/gravitational/teleport/lib/auth/join"
6160
"github.com/gravitational/teleport/lib/auth/machineid/machineidv1"
6261
"github.com/gravitational/teleport/lib/auth/state"
6362
"github.com/gravitational/teleport/lib/auth/testauthority"
6463
"github.com/gravitational/teleport/lib/cloud/azure"
6564
libevents "github.com/gravitational/teleport/lib/events"
6665
"github.com/gravitational/teleport/lib/events/eventstest"
6766
"github.com/gravitational/teleport/lib/fixtures"
67+
"github.com/gravitational/teleport/lib/join/joinclient"
6868
"github.com/gravitational/teleport/lib/kube/token"
6969
"github.com/gravitational/teleport/lib/oidc/fakeissuer"
7070
"github.com/gravitational/teleport/lib/reversetunnelclient"
@@ -154,7 +154,7 @@ func TestRegisterBotCertificateGenerationCheck(t *testing.T) {
154154
require.NoError(t, err)
155155
require.NoError(t, client.CreateToken(ctx, token))
156156

157-
result, err := join.Register(ctx, join.RegisterParams{
157+
result, err := joinclient.Join(ctx, joinclient.JoinParams{
158158
Token: token.GetName(),
159159
ID: state.IdentityID{
160160
Role: types.RoleBot,
@@ -294,7 +294,7 @@ func TestBotJoinAttrs_Kubernetes(t *testing.T) {
294294
require.NoError(t, err)
295295
require.NoError(t, client.CreateToken(ctx, tok))
296296

297-
result, err := join.Register(ctx, join.RegisterParams{
297+
result, err := joinclient.Join(ctx, joinclient.JoinParams{
298298
Token: tok.GetName(),
299299
JoinMethod: types.JoinMethodKubernetes,
300300
ID: state.IdentityID{
@@ -406,7 +406,7 @@ func TestRegisterBotInstance(t *testing.T) {
406406
require.NoError(t, err)
407407
require.NoError(t, client.CreateToken(ctx, token))
408408

409-
result, err := join.Register(ctx, join.RegisterParams{
409+
result, err := joinclient.Join(ctx, joinclient.JoinParams{
410410
Token: token.GetName(),
411411
ID: state.IdentityID{
412412
Role: types.RoleBot,
@@ -552,7 +552,7 @@ func TestRegisterBotCertificateGenerationStolen(t *testing.T) {
552552
require.NoError(t, err)
553553
require.NoError(t, client.CreateToken(ctx, token))
554554

555-
result, err := join.Register(ctx, join.RegisterParams{
555+
result, err := joinclient.Join(ctx, joinclient.JoinParams{
556556
Token: token.GetName(),
557557
ID: state.IdentityID{
558558
Role: types.RoleBot,
@@ -628,7 +628,7 @@ func TestRegisterBotCertificateExtensions(t *testing.T) {
628628
require.NoError(t, err)
629629
require.NoError(t, client.CreateToken(ctx, token))
630630

631-
result, err := join.Register(ctx, join.RegisterParams{
631+
result, err := joinclient.Join(ctx, joinclient.JoinParams{
632632
Token: token.GetName(),
633633
ID: state.IdentityID{
634634
Role: types.RoleBot,
@@ -823,8 +823,8 @@ func TestRegisterBot_RemoteAddr(t *testing.T) {
823823
}
824824

825825
// authClientForRegisterResult is a test helper that creats an auth client for
826-
// the given [*join.RegisterResult].
827-
func authClientForRegisterResult(t *testing.T, ctx context.Context, addr *utils.NetAddr, result *join.RegisterResult) *authclient.Client {
826+
// the given [*joinclient.JoinResult].
827+
func authClientForRegisterResult(t *testing.T, ctx context.Context, addr *utils.NetAddr, result *joinclient.JoinResult) *authclient.Client {
828828
privateKeyPEM, err := keys.MarshalPrivateKey(result.PrivateKey)
829829
require.NoError(t, err)
830830
sshPub, err := ssh.NewPublicKey(result.PrivateKey.Public())
@@ -895,14 +895,14 @@ func instanceIDFromCerts(t *testing.T, certs *proto.Certs) (string, uint64) {
895895
return ident.BotInstanceID, ident.Generation
896896
}
897897

898-
// registerHelper calls `join.Register` with the given token, prefilling params
898+
// registerHelper calls `joinclient.Join` with the given token, prefilling params
899899
// where possible. Overrides may be applied with `fns`.
900900
func registerHelper(
901901
ctx context.Context, token types.ProvisionToken,
902902
addr *utils.NetAddr,
903-
fns ...func(*join.RegisterParams),
904-
) (*join.RegisterResult, error) {
905-
params := join.RegisterParams{
903+
fns ...func(*joinclient.JoinParams),
904+
) (*joinclient.JoinResult, error) {
905+
params := joinclient.JoinParams{
906906
JoinMethod: token.GetJoinMethod(),
907907
Token: token.GetName(),
908908
ID: state.IdentityID{
@@ -918,7 +918,7 @@ func registerHelper(
918918
fn(&params)
919919
}
920920

921-
result, err := join.Register(ctx, params)
921+
result, err := joinclient.Join(ctx, params)
922922
return result, trace.Wrap(err)
923923
}
924924

@@ -1015,7 +1015,7 @@ func TestRegisterBot_BotInstanceRejoin(t *testing.T) {
10151015
require.NoError(t, a.UpsertToken(ctx, awsToken))
10161016

10171017
// Join as a "bot" with both token types.
1018-
k8sResult, err := registerHelper(ctx, k8sToken, addr, func(p *join.RegisterParams) {
1018+
k8sResult, err := registerHelper(ctx, k8sToken, addr, func(p *joinclient.JoinParams) {
10191019
p.KubernetesReadFileFunc = k8sReadFileFunc
10201020
})
10211021
require.NoError(t, err)
@@ -1035,7 +1035,7 @@ func TestRegisterBot_BotInstanceRejoin(t *testing.T) {
10351035
// Rejoin using the k8s client and make sure we're issued certs with the
10361036
// same instance ID.
10371037
k8sClient := authClientForRegisterResult(t, ctx, addr, k8sResult)
1038-
rejoinedK8sResult, err := registerHelper(ctx, k8sToken, addr, func(p *join.RegisterParams) {
1038+
rejoinedK8sResult, err := registerHelper(ctx, k8sToken, addr, func(p *joinclient.JoinParams) {
10391039
p.KubernetesReadFileFunc = k8sReadFileFunc
10401040
p.AuthClient = k8sClient
10411041
})
@@ -1049,7 +1049,7 @@ func TestRegisterBot_BotInstanceRejoin(t *testing.T) {
10491049
// join service, the instance ID must be provided to auth by the proxy as
10501050
// part of the `RegisterUsingTokenRequest`.
10511051
iamClient := authClientForRegisterResult(t, ctx, addr, awsResult)
1052-
rejoinedAWSResult, err := registerHelper(ctx, awsToken, addr, func(p *join.RegisterParams) {
1052+
rejoinedAWSResult, err := registerHelper(ctx, awsToken, addr, func(p *joinclient.JoinParams) {
10531053
p.AuthClient = iamClient
10541054
})
10551055
require.NoError(t, err)
@@ -1229,7 +1229,7 @@ func TestRegisterBotMultipleTokens(t *testing.T) {
12291229
require.NoError(t, err)
12301230
require.NoError(t, client.CreateToken(ctx, tokenB))
12311231

1232-
resultA, err := join.Register(ctx, join.RegisterParams{
1232+
resultA, err := joinclient.Join(ctx, joinclient.JoinParams{
12331233
Token: tokenA.GetName(),
12341234
ID: state.IdentityID{
12351235
Role: types.RoleBot,
@@ -1242,7 +1242,7 @@ func TestRegisterBotMultipleTokens(t *testing.T) {
12421242
initialInstanceA, _ := instanceIDFromCerts(t, certsA)
12431243
require.NotEmpty(t, initialInstanceA)
12441244

1245-
resultB, err := join.Register(ctx, join.RegisterParams{
1245+
resultB, err := joinclient.Join(ctx, joinclient.JoinParams{
12461246
Token: tokenB.GetName(),
12471247
ID: state.IdentityID{
12481248
Role: types.RoleBot,

lib/auth/join/join.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ type RegisterResult struct {
261261
// running on a different host than the auth server. This method requires a
262262
// provision token that will be used to authenticate as an identity that should
263263
// be allowed to join the cluster.
264+
//
265+
// Deprecated: this function is superceded by lib/join/joinclient.Join
266+
//
267+
// TODO(nklaassen): DELETE IN 20
264268
func Register(ctx context.Context, params RegisterParams) (result *RegisterResult, err error) {
265269
ctx, span := tracer.Start(ctx, "Register")
266270
defer func() { tracing.EndSpan(span, err) }()

lib/auth/join_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ import (
3636
"github.com/gravitational/teleport/api/utils/sshutils"
3737
"github.com/gravitational/teleport/lib/auth"
3838
"github.com/gravitational/teleport/lib/auth/authtest"
39-
"github.com/gravitational/teleport/lib/auth/join"
4039
"github.com/gravitational/teleport/lib/auth/machineid/machineidv1"
4140
"github.com/gravitational/teleport/lib/auth/state"
4241
"github.com/gravitational/teleport/lib/auth/testauthority"
4342
"github.com/gravitational/teleport/lib/defaults"
4443
"github.com/gravitational/teleport/lib/events"
44+
"github.com/gravitational/teleport/lib/join/joinclient"
4545
"github.com/gravitational/teleport/lib/tlsca"
4646
"github.com/gravitational/teleport/lib/utils"
4747
)
@@ -299,9 +299,9 @@ func newBotToken(t *testing.T, tokenName, botName string, role types.SystemRole,
299299
return token
300300
}
301301

302-
// TestRegister_Bot tests that a provision token can be used to generate
302+
// TestJoin_Bot tests that a provision token can be used to generate
303303
// renewable certificates for a non-interactive user.
304-
func TestRegister_Bot(t *testing.T) {
304+
func TestJoin_Bot(t *testing.T) {
305305
t.Parallel()
306306
ctx := context.Background()
307307

@@ -369,7 +369,7 @@ func TestRegister_Bot(t *testing.T) {
369369
} {
370370
t.Run(test.desc, func(t *testing.T) {
371371
start := srv.Clock().Now()
372-
result, err := join.Register(ctx, join.RegisterParams{
372+
result, err := joinclient.Join(ctx, joinclient.JoinParams{
373373
Token: test.token.GetName(),
374374
ID: state.IdentityID{
375375
Role: types.RoleBot,
@@ -413,9 +413,9 @@ func TestRegister_Bot(t *testing.T) {
413413
}
414414
}
415415

416-
// TestRegister_Bot_Expiry checks that bot certificate expiry can be set, and
416+
// TestJoin_Bot_Expiry checks that bot certificate expiry can be set, and
417417
// does not exceed the limit.
418-
func TestRegister_Bot_Expiry(t *testing.T) {
418+
func TestJoin_Bot_Expiry(t *testing.T) {
419419
t.Parallel()
420420
ctx := context.Background()
421421

@@ -465,7 +465,7 @@ func TestRegister_Bot_Expiry(t *testing.T) {
465465
tok := newBotToken(t, uuid.NewString(), botName, types.RoleBot, srv.Clock().Now().Add(time.Hour))
466466
require.NoError(t, srv.Auth().UpsertToken(ctx, tok))
467467

468-
result, err := join.Register(ctx, join.RegisterParams{
468+
result, err := joinclient.Join(ctx, joinclient.JoinParams{
469469
Token: tok.GetName(),
470470
ID: state.IdentityID{
471471
Role: types.RoleBot,

lib/auth/machineid/workloadidentityv1/workloadidentityv1_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ import (
5656
"github.com/gravitational/teleport/api/utils/keys"
5757
"github.com/gravitational/teleport/lib/auth/authclient"
5858
"github.com/gravitational/teleport/lib/auth/authtest"
59-
"github.com/gravitational/teleport/lib/auth/join"
6059
"github.com/gravitational/teleport/lib/auth/machineid/workloadidentityv1"
6160
"github.com/gravitational/teleport/lib/auth/state"
6261
"github.com/gravitational/teleport/lib/cryptosuites"
6362
libevents "github.com/gravitational/teleport/lib/events"
6463
"github.com/gravitational/teleport/lib/events/eventstest"
64+
"github.com/gravitational/teleport/lib/join/joinclient"
6565
libjwt "github.com/gravitational/teleport/lib/jwt"
6666
"github.com/gravitational/teleport/lib/modules"
6767
"github.com/gravitational/teleport/lib/oidc/fakeissuer"
@@ -298,7 +298,7 @@ func TestIssueWorkloadIdentityE2E(t *testing.T) {
298298
require.NoError(t, err)
299299

300300
// With the basic setup complete, we can now "fake" a join.
301-
botCerts, err := join.Register(ctx, join.RegisterParams{
301+
botCerts, err := joinclient.Join(ctx, joinclient.JoinParams{
302302
Token: token.GetName(),
303303
JoinMethod: types.JoinMethodKubernetes,
304304
ID: state.IdentityID{

lib/auth/storage/storage.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,31 @@ func readHostIDFromStorages(ctx context.Context, dataDir string, kubeBackend sta
382382
return hostID, trace.Wrap(err)
383383
}
384384

385+
// PersistAssignedHostID writes an assigned host ID to state storage and the
386+
// host_uuid file. This should not be called in the same process as
387+
// ReadOrGenerateHostID, it is intended to persist a host UUID assigned by the
388+
// Auth service that was not generated locally. With the new auth-assigned host
389+
// persisted to storage to maintain compatibility with any other processes that
390+
// UUID flow the agent doesn't even need to read the host ID, it is only
391+
// may read it.
392+
func (p *ProcessStorage) PersistAssignedHostID(ctx context.Context, cfg *servicecfg.Config, hostID string) error {
393+
if p.stateStorage != nil {
394+
if _, err := p.stateStorage.Put(
395+
ctx,
396+
backend.Item{
397+
Key: backend.NewKey(hostid.FileName),
398+
Value: []byte(hostID),
399+
},
400+
); err != nil {
401+
return trace.Wrap(err, "persisting host ID to state storage")
402+
}
403+
}
404+
if err := hostid.WriteFile(cfg.DataDir, hostID); err != nil {
405+
return trace.Wrap(err, "persisting host ID to file")
406+
}
407+
return nil
408+
}
409+
385410
// persistHostIDToStorages writes the host ID to local data and to
386411
// Kubernetes Secret if this process is running on a Kubernetes Cluster.
387412
func persistHostIDToStorages(ctx context.Context, cfg *servicecfg.Config, hostID string, kubeBackend stateBackend) error {

0 commit comments

Comments
 (0)