Skip to content

Commit 92c3e2c

Browse files
committed
refactor: trim domains for spaces
1 parent f2ac010 commit 92c3e2c

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

services/api/src/resources/routes/helpers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ export const Helpers = (sqlClientPool: Pool) => {
2929
Sql.selectRoutesByDomainAndEnvironmentID(domain, environmentId)
3030
)
3131
if (routes.length == 0) {
32-
logger.error(`Route doesn't exist on this environment`)
33-
return null
32+
throw new Error(`Route doesn't exist on this environment`);
3433
}
3534
const route = routes[0]
3635
await query(

services/api/src/resources/routes/resolvers.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import { isDNS1123Subdomain } from '../../util/func';
1212
import { AuditType, RouteSource, RouteType } from '@lagoon/commons/dist/types';
1313
import { logger } from '../../loggers/logger';
1414

15+
// check array for duplicates that are trimmed and lowercased
1516
function hasDuplicates(arr) {
16-
return new Set(arr).size !== arr.length;
17+
const normalized = arr.map(item => item.trim().toLowerCase());
18+
return new Set(normalized).size !== normalized.length;
1719
}
1820

1921
/*
@@ -55,6 +57,9 @@ export const addRouteToProject: ResolverFn = async (
5557
project: projectId
5658
});
5759

60+
// trim spaces from domain name
61+
const domainName = domain.trim()
62+
5863
let environmentData;
5964
let environmentId = null;
6065
// let environmentServices;
@@ -68,11 +73,11 @@ export const addRouteToProject: ResolverFn = async (
6873
// check the route doesn't already exist in this project as either a top level route, or an alternative domain on another route
6974
const exists = await query(
7075
sqlClientPool,
71-
Sql.selectRouteByDomainAndProjectID(domain, projectId)
76+
Sql.selectRouteByDomainAndProjectID(domainName, projectId)
7277
)
7378
const exists2 = await query(
7479
sqlClientPool,
75-
Sql.selectRouteAlternativeDomainsByDomainAndProjectID(domain, projectId)
80+
Sql.selectRouteAlternativeDomainsByDomainAndProjectID(domainName, projectId)
7681
)
7782

7883
// fail if the route already exists somewhere in the project
@@ -81,8 +86,8 @@ export const addRouteToProject: ResolverFn = async (
8186
}
8287

8388
// check if the domain is valid dns subdomain
84-
if (!isDNS1123Subdomain(domain)) {
85-
throw Error(`'${domain}' is not a valid domain`)
89+
if (!isDNS1123Subdomain(domainName)) {
90+
throw Error(`'${domainName}' is not a valid domain`)
8691
}
8792

8893
if (environment && !service) {
@@ -104,7 +109,9 @@ export const addRouteToProject: ResolverFn = async (
104109
// fail if the domain is provided more than once
105110
if (alternativeNames !== undefined) {
106111
for (const d of alternativeNames) {
107-
if (d == domain) {
112+
// trim spaces from the alternate domain as part of comparison check
113+
const altDomain = d.trim()
114+
if (altDomain == domainName) {
108115
throw Error(`Main domain included in alternate domains`)
109116
}
110117
}
@@ -168,7 +175,7 @@ export const addRouteToProject: ResolverFn = async (
168175
sqlClientPool,
169176
Sql.insertRoute({
170177
id,
171-
domain,
178+
domain: domainName,
172179
project: projectId,
173180
environment: environmentId,
174181
service,
@@ -197,11 +204,13 @@ export const addRouteToProject: ResolverFn = async (
197204
// add the alternate domains
198205
if (alternativeNames !== undefined) {
199206
for (const d of alternativeNames) {
207+
// trim spaces from domain name
208+
const altDomain = d.trim()
200209
await query(
201210
sqlClientPool,
202211
Sql.insertRouteAlternativeDomain({
203212
rid: insertId,
204-
domain: d,
213+
domain: altDomain,
205214
})
206215
);
207216
}
@@ -277,7 +286,7 @@ export const updateRouteOnProject: ResolverFn = async (
277286
break;
278287
}
279288
// otherwise reject the update
280-
throw Error(`Cannot update lagoon yaml managed routes`)
289+
throw Error(`Cannot update route managed by lagoon.yml`)
281290
case RouteSource.AUTOGENERATED:
282291
// reject update from general users
283292
throw Error(`Cannot update autogenerated routes`)
@@ -566,6 +575,13 @@ export const removeRouteFromEnvironment: ResolverFn = async (
566575
const ret = await query(sqlClientPool, Sql.selectRouteByID(routeId));
567576
const route = ret[0];
568577

578+
if (route.source.toLowerCase() === RouteSource.YAML) {
579+
throw Error(`This route cannot be removed from the environment as it is managed by a lagoon.yml file`)
580+
}
581+
if (route.source.toLowerCase() === RouteSource.AUTOGENERATED) {
582+
throw Error(`Cannot remove autogenerated routes from an environment using this endpoint`)
583+
}
584+
569585
const auditLog: AuditLog = {
570586
resource: {
571587
id: projectData.id.toString(),
@@ -644,7 +660,7 @@ export const addRouteAlternativeDomains: ResolverFn = async (
644660
)
645661
// if the domains provided don't already exist, then add them
646662
if (exists.length > 0 || exists2.length > 0) {
647-
throw Error(`Route exists in this project already`)
663+
throw Error(`Route already exists in this project`)
648664
}
649665
// check if the domain is valid dns subdomain
650666
if (!isDNS1123Subdomain(d)) {
@@ -1039,7 +1055,7 @@ export const deleteRoute: ResolverFn = async (
10391055
throw new Error(`Cannot delete autogenerated routes, you must modify the project or environment autogenerated route configuration`);
10401056
}
10411057
if (route.source.toLowerCase() == RouteSource.YAML) {
1042-
throw new Error(`Cannot delete routes that are managed by lagoon.yml, either modify the route in the api or delete it from the lagoon.yml file.`);
1058+
throw new Error(`Cannot delete routes that are managed by a lagoon.yml file, either modify the route in the api or delete it from the lagoon.yml file.`);
10431059
}
10441060
}
10451061
// @TODO: do we want to block deletion of routes if they are attached to an environment?

0 commit comments

Comments
 (0)