Skip to content

Commit 0e9aa24

Browse files
committed
fix(credentials): keep default-region token credentials as they were and share region definitions
- A credential on its provider's default region or host keeps the bare service display name, and a vendor region host that fails to resolve is reported as the vendor being unavailable, not a typed host that was not found. - defaultValue is honored on the domain field only; providerWorkspaceId keeps its required, non-defaulted handling everywhere, so a blank reconnect can never reset it. - One defineCredentialRegions factory now holds each vendor's region ids, hosts, labels, default, API base, and hidden tool param (LaunchDarkly, LangSmith, New Relic, SendGrid, Sentry, Square); the internal SendGrid and Square schemas validate against it, and the New Relic descriptor and block take their options from it. - One isTokenServiceAccountFieldRequired predicate replaces the inline defaulted-is-optional checks. - Hex refuses its known non-workspace hex.tech labels (status, learn, docs, www, help, community, blog), and the pattern's comment no longer claims more than it guarantees. - Drops a mock-only and two pass-through tests; adds Sentry to the default-region tables.
1 parent bcaf72d commit 0e9aa24

107 files changed

Lines changed: 606 additions & 649 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/sim/app/workspace/[workspaceId]/integrations/components/connect-service-account-modal/token-service-account-modal.tsx‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { ServiceAccountSecretFields } from '@/lib/credentials/service-accou
2222
import {
2323
CLIENT_CREDENTIALS_AUTH_METHOD,
2424
getTokenServiceAccountErrorMessage,
25+
isTokenServiceAccountFieldRequired,
2526
type TokenServiceAccountDescriptor,
2627
type TokenServiceAccountField,
2728
} from '@/lib/credentials/token-service-accounts/descriptors'
@@ -119,18 +120,17 @@ function TokenServiceAccountModalForm({
119120
const domainField = descriptor.fields.find((field) => field.id === 'domain')
120121
const workspaceField = descriptor.fields.find((field) => field.id === 'providerWorkspaceId')
121122
/**
122-
* A new connection starts on a field's default (the region or host it had before the field
123+
* A new connection starts on the domain's default (the region or host it had before the field
123124
* existed). A reconnect starts blank, which keeps the stored value: the server carries it
124125
* forward, since the stored one is never returned to the client.
125126
*/
126-
const initialValue = (field: TokenServiceAccountField | undefined) =>
127-
credentialId ? '' : (field?.defaultValue ?? '')
127+
const initialDomain = credentialId ? '' : (domainField?.defaultValue ?? '')
128128

129129
const [apiToken, setApiToken] = useState('')
130130
const [clientId, setClientId] = useState('')
131131
const [clientSecret, setClientSecret] = useState('')
132-
const [domain, setDomain] = useState(() => initialValue(domainField))
133-
const [providerWorkspaceId, setProviderWorkspaceId] = useState(() => initialValue(workspaceField))
132+
const [domain, setDomain] = useState(initialDomain)
133+
const [providerWorkspaceId, setProviderWorkspaceId] = useState('')
134134
const [displayName, setDisplayName] = useState(initialDisplayName ?? '')
135135
const [description, setDescription] = useState(initialDescription ?? '')
136136
const [error, setError] = useState<string | null>(null)
@@ -141,14 +141,13 @@ function TokenServiceAccountModalForm({
141141
const trimmedToken = apiToken.trim()
142142
const normalizedDomain = domainField?.url ? domain.trim() : normalizeDomainInput(domain)
143143
const trimmedWorkspaceId = providerWorkspaceId.trim()
144-
const isRequired = (field: TokenServiceAccountField) => field.defaultValue === undefined
145144
const isPending =
146145
createCredential.isPending || updateCredential.isPending || submitPending === true
147146
const isDisabled =
148147
!authMethod ||
149148
(usesClientCredentials ? !clientId.trim() || !clientSecret.trim() : !trimmedToken) ||
150-
Boolean(domainField && isRequired(domainField) && !normalizedDomain) ||
151-
Boolean(workspaceField && isRequired(workspaceField) && !trimmedWorkspaceId) ||
149+
Boolean(domainField && isTokenServiceAccountFieldRequired(domainField) && !normalizedDomain) ||
150+
(Boolean(workspaceField) && !trimmedWorkspaceId) ||
152151
isPending
153152

154153
const hintFor = (field: TokenServiceAccountField, value: string): string | undefined => {
@@ -167,7 +166,7 @@ function TokenServiceAccountModalForm({
167166
if (error) setError(null)
168167
}
169168
const placeholder =
170-
credentialId && !isRequired(field)
169+
credentialId && !isTokenServiceAccountFieldRequired(field)
171170
? `Keep the current ${field.label.toLowerCase()}`
172171
: field.placeholder
173172
if (field.options) {
@@ -180,7 +179,7 @@ function TokenServiceAccountModalForm({
180179
options={field.options}
181180
placeholder={placeholder}
182181
align='start'
183-
required={isRequired(field)}
182+
required={isTokenServiceAccountFieldRequired(field)}
184183
/>
185184
)
186185
}
@@ -192,7 +191,7 @@ function TokenServiceAccountModalForm({
192191
onChange={onChange}
193192
placeholder={placeholder}
194193
autoComplete='off'
195-
required={isRequired(field)}
194+
required={isTokenServiceAccountFieldRequired(field)}
196195
error={formatError}
197196
/>
198197
)
@@ -210,9 +209,7 @@ function TokenServiceAccountModalForm({
210209
clientSecret: clientSecret.trim(),
211210
}
212211
: { apiToken: tokenField?.preserveWhitespace ? apiToken : trimmedToken }),
213-
...(workspaceField && trimmedWorkspaceId
214-
? { providerWorkspaceId: trimmedWorkspaceId }
215-
: {}),
212+
...(workspaceField ? { providerWorkspaceId: trimmedWorkspaceId } : {}),
216213
...(domainField && normalizedDomain ? { domain: normalizedDomain } : {}),
217214
}
218215
if (onSubmitCredentials) {

‎apps/sim/blocks/blocks/new_relic.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { BlockConfig, BlockMeta } from '@/blocks/types'
44
import { AuthMode, IntegrationType } from '@/blocks/types'
55
import { withSavedCredential } from '@/blocks/utils'
66
import type { NewRelicCustomAttributes } from '@/tools/new_relic/types'
7+
import { NEW_RELIC_REGIONS } from '@/tools/new_relic/utils'
78

89
function parseCustomAttributes(value: unknown): NewRelicCustomAttributes | undefined {
910
if (!value) return undefined
@@ -77,11 +78,7 @@ export const NewRelicBlock: BlockConfig = {
7778
id: 'region',
7879
title: 'Region',
7980
type: 'dropdown',
80-
options: [
81-
{ label: 'US', id: 'us' },
82-
{ label: 'EU', id: 'eu' },
83-
{ label: 'JP', id: 'jp' },
84-
],
81+
options: NEW_RELIC_REGIONS.options.map(({ value, label }) => ({ id: value, label })),
8582
value: () => 'us',
8683
},
8784
{

‎apps/sim/lib/credentials/orchestration/index.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import {
5050
verifyAndBuildServiceAccountSecret,
5151
} from '@/lib/credentials/service-account-secret'
5252
import {
53-
getTokenServiceAccountFieldDefault,
53+
getTokenServiceAccountDomainDefault,
5454
SNOWFLAKE_SERVICE_ACCOUNT_PROVIDER_ID,
5555
} from '@/lib/credentials/token-service-accounts/descriptors'
5656
import { TokenServiceAccountValidationError } from '@/lib/credentials/token-service-accounts/errors'
@@ -370,8 +370,7 @@ export async function updateCredentialRecord(
370370
// A token provider's defaulted region or host is left blank on a reconnect that keeps it;
371371
// filling the default instead would move an EU or sandbox credential to the default host.
372372
const needsStoredDomain =
373-
!params.domain?.trim() &&
374-
getTokenServiceAccountFieldDefault(providerId, 'domain') !== undefined
373+
!params.domain?.trim() && getTokenServiceAccountDomainDefault(providerId) !== undefined
375374
// A keyless AWS role, Databricks principal, Microsoft app, Google service account, or
376375
// Snowflake service user keeps its trusted subject across reconnects.
377376
const needsStoredBoundSubject =

‎apps/sim/lib/credentials/service-account-fields.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('required fields for a service-account create', () => {
2020
'square-service-account',
2121
'sendgrid-service-account',
2222
'hex-service-account',
23+
'sentry-service-account',
2324
])(
2425
'lets a %s create omit the region or host it defaults, as it could before one existed',
2526
(providerId) => {

‎apps/sim/lib/credentials/service-account-secret.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,68 @@ describe('a token provider whose region or host defaults to the one it always us
927927
})
928928
})
929929

930+
it.each([
931+
['launchdarkly-service-account', 'LaunchDarkly'],
932+
['langsmith-service-account', 'LangSmith'],
933+
['square-service-account', 'Square'],
934+
['sendgrid-service-account', 'SendGrid'],
935+
['hex-service-account', 'Hex'],
936+
['sentry-service-account', 'Sentry'],
937+
])(
938+
'names a %s credential on its default host %s, as before the field existed',
939+
async (providerId, label) => {
940+
const result = await verifyAndBuildServiceAccountSecret(providerId, { apiToken: 'token-1' })
941+
942+
expect(result.displayName).toBe(label)
943+
}
944+
)
945+
946+
it.each([
947+
['sentry-service-account', 'de', 'Sentry (de)'],
948+
['hex-service-account', 'eu.hex.tech', 'Hex (eu.hex.tech)'],
949+
])('names a %s credential in %s after it', async (providerId, domain, displayName) => {
950+
const result = await verifyAndBuildServiceAccountSecret(providerId, {
951+
apiToken: 'token-1',
952+
domain,
953+
})
954+
955+
expect(result.displayName).toBe(displayName)
956+
})
957+
958+
const hostNotFound = () =>
959+
Object.assign(new TypeError('fetch failed'), { cause: { code: 'ENOTFOUND' } })
960+
961+
it.each([
962+
['sendgrid-service-account', undefined],
963+
['sendgrid-service-account', 'eu'],
964+
['sentry-service-account', 'de'],
965+
['launchdarkly-service-account', 'federal'],
966+
['hex-service-account', undefined],
967+
])(
968+
'reports a %s vendor host (%s) that does not resolve as the vendor being unavailable',
969+
async (providerId, domain) => {
970+
fetchMock.mockRejectedValue(hostNotFound())
971+
972+
await expect(
973+
verifyAndBuildServiceAccountSecret(providerId, {
974+
apiToken: 'token-1',
975+
...(domain === undefined ? {} : { domain }),
976+
})
977+
).rejects.toMatchObject({ code: 'provider_unavailable' })
978+
}
979+
)
980+
981+
it('reports a typed Hex host that does not resolve as a host not found', async () => {
982+
fetchMock.mockRejectedValue(hostNotFound())
983+
984+
await expect(
985+
verifyAndBuildServiceAccountSecret('hex-service-account', {
986+
apiToken: 'token-1',
987+
domain: 'acme.hex.tech',
988+
})
989+
).rejects.toMatchObject({ code: 'site_not_found' })
990+
})
991+
930992
it.each([
931993
['launchdarkly-service-account', 'app.launchdarkly.example.com'],
932994
['langsmith-service-account', 'eu.api.smith.langchain.com.example.com'],
@@ -935,6 +997,8 @@ describe('a token provider whose region or host defaults to the one it always us
935997
['hex-service-account', 'hex.tech.example.com'],
936998
['hex-service-account', 'acme.hex.tech@example.com'],
937999
['hex-service-account', 'a.b.hex.tech'],
1000+
['hex-service-account', 'status.hex.tech'],
1001+
['hex-service-account', 'learn.hex.tech'],
9381002
['sentry-service-account', 'de.sentry.io.example.com'],
9391003
])(
9401004
'refuses %s with an unlisted region or host %s without sending the token',

‎apps/sim/lib/credentials/service-account-secret.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import { getTokenClientCredentialMinter } from '@/lib/credentials/token-service-
5555
import {
5656
CLIENT_CREDENTIALS_AUTH_METHOD,
5757
getTokenServiceAccountDescriptor,
58+
isTokenServiceAccountFieldRequired,
5859
isTokenServiceAccountProviderId,
5960
SNOWFLAKE_SERVICE_ACCOUNT_PROVIDER_ID,
6061
TOKEN_SERVICE_ACCOUNT_SECRET_TYPE,
@@ -581,16 +582,15 @@ async function buildTokenServiceAccountSecret(
581582
const domainField = descriptor.fields.find((field) => field.id === 'domain')
582583
const domain = fields.domain?.trim() || domainField?.defaultValue
583584
const requiresDomain = Boolean(domainField)
584-
const workspaceField = descriptor.fields.find((field) => field.id === 'providerWorkspaceId')
585-
const providerWorkspaceId = fields.providerWorkspaceId?.trim() || workspaceField?.defaultValue
586-
const requiresWorkspace = Boolean(workspaceField)
585+
const providerWorkspaceId = fields.providerWorkspaceId?.trim()
586+
const requiresWorkspace = descriptor.fields.some((field) => field.id === 'providerWorkspaceId')
587587
if (
588588
!apiToken ||
589589
!apiToken.trim() ||
590590
(requiresDomain && !domain) ||
591591
(requiresWorkspace && !providerWorkspaceId)
592592
) {
593-
const requiredFields = descriptor.fields.filter((field) => field.defaultValue === undefined)
593+
const requiredFields = descriptor.fields.filter(isTokenServiceAccountFieldRequired)
594594
throw new ServiceAccountSecretError(
595595
`${requiredFields.map((field) => field.id).join(' and ')} ${requiredFields.length > 1 ? 'are' : 'is'} required for ${descriptor.serviceLabel} service account credentials`
596596
)

0 commit comments

Comments
 (0)