Skip to content

Commit

Permalink
Allow adding app server labels from join token for install.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
kimlisa committed Jan 3, 2025
1 parent b455a90 commit b22aad1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/web/join_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,13 +630,20 @@ func getJoinScript(ctx context.Context, settings scriptSettings, m nodeAPIGetter
var buf bytes.Buffer
// If app install mode is requested but parameters are blank for some reason,
// we need to return an error.
var appServerResourceLabels []string
if settings.appInstallMode {
if errs := validation.IsDNS1035Label(settings.appName); len(errs) > 0 {
return "", trace.BadParameter("appName %q must be a valid DNS subdomain: https://goteleport.com/docs/application-access/guides/connecting-apps/#application-name", settings.appName)
}
if !appURIPattern.MatchString(settings.appURI) {
return "", trace.BadParameter("appURI %q contains invalid characters", settings.appURI)
}

suggestedLabels := token.GetSuggestedLabels()
appServerResourceLabels, err = scripts.MarshalLabelsYAML(suggestedLabels, 6)
if err != nil {
return "", trace.Wrap(err)
}
}

if settings.discoveryInstallMode {
Expand Down Expand Up @@ -694,6 +701,7 @@ func getJoinScript(ctx context.Context, settings scriptSettings, m nodeAPIGetter
"db_service_resource_labels": dbServiceResourceLabels,
"discoveryInstallMode": settings.discoveryInstallMode,
"discoveryGroup": shsprintf.EscapeDefaultContext(settings.discoveryGroup),
"appServerResourceLabels": appServerResourceLabels,
})
if err != nil {
return "", trace.Wrap(err)
Expand Down
12 changes: 12 additions & 0 deletions lib/web/join_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,18 @@ func TestGetNodeJoinScript(t *testing.T) {
require.Contains(t, script, fmt.Sprintf("%s=%s", types.InternalResourceIDLabel, internalResourceID))
},
},
{
desc: "app server labels",
settings: scriptSettings{token: validToken, appInstallMode: true, appName: "app-name", appURI: "app-uri"},
errAssert: require.NoError,
extraAssertions: func(script string) {
require.Contains(t, script, `APP_NAME='app-name'`)
require.Contains(t, script, `APP_URI='app-uri'`)
require.Contains(t, script, `public_addr`)
require.Contains(t, script, ` labels:`)
require.Contains(t, script, fmt.Sprintf("%s=%s", types.InternalResourceIDLabel, internalResourceID))
},
},
} {
t.Run(test.desc, func(t *testing.T) {
script, err := getJoinScript(context.Background(), test.settings, m)
Expand Down
3 changes: 3 additions & 0 deletions lib/web/scripts/node-join/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ app_service:
- name: "${APP_NAME}"
uri: "${APP_URI}"
public_addr: ${APP_PUBLIC_ADDR}
labels:{{range $index, $line := .appServerResourceLabels}}
{{$line -}}
{{end}}
EOF
}
# installs the provided teleport config (for database service)
Expand Down

0 comments on commit b22aad1

Please sign in to comment.