Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v17] Fetch RequiredApps when resolving app with fqdnHint only #51873

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions lib/web/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4674,6 +4674,71 @@ func TestApplicationWebSessionsDeletedAfterLogout(t *testing.T) {
require.Empty(t, collectAppSessions(context.Background()))
}

func TestGetAppDetails(t *testing.T) {
ctx := context.Background()
s := newWebSuite(t)
pack := s.authPack(t, "[email protected]")

// Register an application called "api".
apiApp, err := types.NewAppV3(types.Metadata{
Name: "api",
}, types.AppSpecV3{
URI: "http://127.0.0.1:8080",
PublicAddr: "api.example.com",
})
require.NoError(t, err)
server, err := types.NewAppServerV3FromApp(apiApp, "host", uuid.New().String())
require.NoError(t, err)
_, err = s.server.Auth().UpsertApplicationServer(s.ctx, server)
require.NoError(t, err)

// Register an application called "client" and have "api" required.
clientApp, err := types.NewAppV3(types.Metadata{
Name: "client",
}, types.AppSpecV3{
URI: "http://127.0.0.1:8080",
PublicAddr: "client.example.com",
RequiredAppNames: []string{"api"},
})
require.NoError(t, err)
server2, err := types.NewAppServerV3FromApp(clientApp, "host", uuid.New().String())
require.NoError(t, err)
_, err = s.server.Auth().UpsertApplicationServer(s.ctx, server2)
require.NoError(t, err)

clientFQDN := "client.example.com"

tests := []struct {
name string
endpoint string
}{
{
name: "request app details with clientName and publicAddr",
endpoint: pack.clt.Endpoint("webapi", "apps", clientFQDN, s.server.ClusterName(), clientApp.GetPublicAddr()),
},
{
name: "request app details with fqdn only",
endpoint: pack.clt.Endpoint("webapi", "apps", clientFQDN),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

re, err := pack.clt.Get(ctx, tc.endpoint, url.Values{})
require.NoError(t, err)
resp := GetAppDetailsResponse{}

require.NoError(t, json.Unmarshal(re.Bytes(), &resp))
require.Equal(t, GetAppDetailsResponse{
FQDN: "client.example.com",
RequiredAppFQDNs: []string{"api.example.com", "client.example.com"},
}, resp)
})
}
}

func TestGetWebConfig_WithEntitlements(t *testing.T) {
ctx := context.Background()
env := newWebPack(t, 1)
Expand Down
12 changes: 10 additions & 2 deletions lib/web/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,24 @@ func (h *Handler) getAppDetails(w http.ResponseWriter, r *http.Request, p httpro
requiredAppNames := result.App.GetRequiredAppNames()

if !isRedirectFlow {
// TODO (avatus) this would be nice if the string in the RequiredApps spec was the fqdn of the required app
// so we could skip the resolution step all together but this would break existing configs.

// if clusterName is not supplied in the params, the initial app must have been fetched with fqdn hint only.
// We can use the clusterName of the initially resolved app
if clusterName == "" {
clusterName = result.ClusterName
}
for _, required := range requiredAppNames {
res, err := h.resolveApp(r.Context(), ctx, ResolveAppParams{ClusterName: clusterName, AppName: required})
if err != nil {
h.log.Errorf("Error getting app details for %s, a required app for %s", required, result.App.GetName())
continue
}
resp.RequiredAppFQDNs = append(resp.RequiredAppFQDNs, res.App.GetPublicAddr())
resp.RequiredAppFQDNs = append(resp.RequiredAppFQDNs, res.FQDN)
}
// append self to end of required apps so that it can be the final entry in the redirect "chain".
resp.RequiredAppFQDNs = append(resp.RequiredAppFQDNs, result.App.GetPublicAddr())
resp.RequiredAppFQDNs = append(resp.RequiredAppFQDNs, result.FQDN)
}

return resp, nil
Expand Down
Loading