Skip to content
Open
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
22 changes: 22 additions & 0 deletions infrastructure/authentication/auth_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,33 @@ func getPrioritizedApiUrl(customUrl string, engineUrl string) string {
return engineUrl
}

// #IDE-1488 Fix for production debugging issue on the EU server.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this comment needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope of the comment is to link the bug fix to a ticket.
Sometimes when fixing a bug, the change does not make sense after some time and this would help avoid removing or refactoring it.
If this does not fit team's conventions, I will remove it.

if isApiSubdomain(defaultUrl, customUrl) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the logical reasoning for this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to only apply the switch between URLs for those particular cases.

return defaultUrl
}

// Otherwise, return the custom URL set by the user.
// FedRAMP and single tenant environments.
return customUrl
}

func isApiSubdomain(domain, subdomain string) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read the code correctly, this method returns, whether the given domain starts with api. I do not understand what we want to achieve with the code from L178ff.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method tests if a given subdomain URL is a region subdomain of the parent domain, but only do it if both URLs start with api. to avoid altering existing behaviour for other types of URLs.

domain = strings.TrimPrefix(domain, "https://")
subdomain = strings.TrimPrefix(subdomain, "https://")

if !strings.HasPrefix(domain, "api.") || !strings.HasPrefix(subdomain, "api.") {
return false
}

domain = strings.TrimPrefix(domain, "api.")
subdomain = strings.TrimPrefix(subdomain, "api.")

domain = strings.ToLower(strings.TrimSpace(domain))
subdomain = strings.ToLower(strings.TrimSpace(subdomain))

return strings.HasSuffix(subdomain, "."+domain)
}

func (a *AuthenticationServiceImpl) UpdateCredentials(newToken string, sendNotification bool, updateApiUrl bool) {
a.m.Lock()
defer a.m.Unlock()
Expand Down
6 changes: 6 additions & 0 deletions infrastructure/authentication/auth_service_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ func TestGetApiUrl(t *testing.T) {
engineUrl: "",
expectedResult: customUrl,
},
{
name: "Default URL when custom URL is subdomain",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? This does not seem right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only apply the switch in the case when both URLs start with api and the custom one is actually a region of default one.
All the existing tests are still passing, no change in existing behavior and I added an extra test for this exact situation.

customUrl: "https://api.eu.snyk.io",
engineUrl: "",
expectedResult: defaultUrl, // equals to "https://api.snyk.io"
},
}

for _, tt := range tests {
Expand Down
Loading