-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(assets): Configure token exchange for Properties API (#6131)
* Refactor assets configuration * Configure token exchange in properties api * Remove properties scope from service portal * Charts * Feedback * Fixes * Format Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4f24b0a
commit 13ff809
Showing
16 changed files
with
154 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 7 additions & 36 deletions
43
libs/api/domains/assets/src/lib/api-domains-assets.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,13 @@ | ||
import { DynamicModule, Module } from '@nestjs/common' | ||
import fetch from 'isomorphic-fetch' | ||
import { Module } from '@nestjs/common' | ||
import { AuthModule } from '@island.is/auth-nest-tools' | ||
import { AssetsClientModule } from '@island.is/clients/assets' | ||
import { FasteignirApi, Configuration } from '@island.is/clients/assets' | ||
import { | ||
createXRoadAPIPath, | ||
XRoadMemberClass, | ||
} from '@island.is/shared/utils/server' | ||
|
||
import { AssetsXRoadResolver } from './api-domains-assets.resolver' | ||
import { AssetsXRoadService } from './api-domains-assets.service' | ||
|
||
export interface AssetsXRoadConfig { | ||
xRoadBasePathWithEnv: string | ||
xRoadAssetsMemberCode: string | ||
xRoadAssetsApiPath: string | ||
xRoadClientId: string | ||
} | ||
|
||
@Module({}) | ||
export class AssetsModule { | ||
static register(config: AssetsXRoadConfig): DynamicModule { | ||
return { | ||
module: AssetsModule, | ||
providers: [AssetsXRoadResolver, AssetsXRoadService], | ||
imports: [ | ||
AssetsClientModule.register({ | ||
xRoadPath: createXRoadAPIPath( | ||
config.xRoadBasePathWithEnv, | ||
XRoadMemberClass.GovernmentInstitution, | ||
config.xRoadAssetsMemberCode, | ||
config.xRoadAssetsApiPath, | ||
), | ||
xRoadClient: config.xRoadClientId, | ||
}), | ||
AuthModule, | ||
], | ||
exports: [AssetsXRoadService], | ||
} | ||
} | ||
} | ||
@Module({ | ||
providers: [AssetsXRoadResolver, AssetsXRoadService], | ||
imports: [AssetsClientModule, AuthModule], | ||
exports: [AssetsXRoadService], | ||
}) | ||
export class AssetsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
libs/api/domains/assets/src/lib/authorization-identity.middleware.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
export { | ||
AssetsClientModule, | ||
ModuleConfig as AssetsModuleConfig, | ||
} from './lib/assets.module' | ||
export { AssetsClientModule } from './lib/assets.module' | ||
export { AssetsClientConfig } from './lib/assets.config' | ||
export * from '../gen/fetch' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Provider } from '@nestjs/common/interfaces/modules/provider.interface' | ||
import nodeFetch, { Request } from 'node-fetch' | ||
|
||
import { | ||
createEnhancedFetch, | ||
EnhancedFetchOptions, | ||
} from '@island.is/clients/middlewares' | ||
import { | ||
ConfigType, | ||
LazyDuringDevScope, | ||
XRoadConfig, | ||
} from '@island.is/nest/config' | ||
|
||
import { Configuration, FasteignirApi } from '../../gen/fetch' | ||
import { AssetsClientConfig } from './assets.config' | ||
|
||
export const PropertiesApiProvider: Provider<FasteignirApi> = { | ||
provide: FasteignirApi, | ||
scope: LazyDuringDevScope, | ||
useFactory: ( | ||
xroadConfig: ConfigType<typeof XRoadConfig>, | ||
config: ConfigType<typeof AssetsClientConfig>, | ||
) => | ||
new FasteignirApi( | ||
new Configuration({ | ||
fetchApi: createEnhancedFetch({ | ||
name: 'clients-assets', | ||
...config.fetch, | ||
fetch: (url, init) => { | ||
// The Properties API expects two different authorization headers for some reason. | ||
const request = new Request(url, init) | ||
request.headers.set( | ||
'authorization-identity', | ||
request.headers.get('authorization') ?? '', | ||
) | ||
return nodeFetch(request) | ||
}, | ||
} as EnhancedFetchOptions), | ||
// TODO: Remove ^ "as EnhancedFetchOptions" after making API projects strict TS. | ||
basePath: `${xroadConfig.xRoadBasePath}/r1/${config.xRoadServicePath}`, | ||
headers: { | ||
'X-Road-Client': xroadConfig.xRoadClient, | ||
Accept: 'application/json', | ||
}, | ||
}), | ||
), | ||
inject: [XRoadConfig.KEY, AssetsClientConfig.KEY], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { NationalRegistryScope } from '@island.is/auth/scopes' | ||
import { defineConfig } from '@island.is/nest/config' | ||
import * as z from 'zod' | ||
|
||
const schema = z.object({ | ||
xRoadServicePath: z.string(), | ||
fetch: z.object({ | ||
timeout: z.number().int(), | ||
autoAuth: z | ||
.object({ | ||
mode: z.enum(['token', 'tokenExchange', 'auto']), | ||
issuer: z.string(), | ||
clientId: z.string(), | ||
clientSecret: z.string(), | ||
scope: z.array(z.string()), | ||
}) | ||
.optional(), | ||
}), | ||
}) | ||
|
||
export const AssetsClientConfig = defineConfig<z.infer<typeof schema>>({ | ||
name: 'AssetsClient', | ||
schema, | ||
load(env) { | ||
const clientSecret = env.optional('XROAD_PROPERTIES_CLIENT_SECRET') | ||
return { | ||
xRoadServicePath: env.required( | ||
'XROAD_PROPERTIES_SERVICE_PATH', | ||
'IS-DEV/GOV/10001/SKRA-Protected/Fasteignir-v1', | ||
), | ||
fetch: { | ||
timeout: env.optionalJSON('XROAD_PROPERTIES_TIMEOUT') ?? 10000, | ||
autoAuth: clientSecret | ||
? { | ||
mode: 'tokenExchange', | ||
issuer: env.required( | ||
'IDENTITY_SERVER_ISSUER_URL', | ||
'https://identity-server.dev01.devland.is', | ||
), | ||
clientId: | ||
env.optional('XROAD_PROPERTIES_CLIENT_ID') ?? | ||
'@island.is/clients/national-registry', | ||
clientSecret, | ||
scope: env.optionalJSON('XROAD_PROPERTIES_SCOPE') ?? [ | ||
NationalRegistryScope.properties, | ||
'api_resource.scope', | ||
], | ||
} | ||
: undefined, | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,8 @@ | ||
import { DynamicModule } from '@nestjs/common' | ||
import { createEnhancedFetch } from '@island.is/clients/middlewares' | ||
import type { EnhancedFetchOptions } from '@island.is/clients/middlewares' | ||
|
||
import { Configuration, FasteignirApi } from '../../gen/fetch' | ||
|
||
export interface ModuleConfig { | ||
xRoadPath?: string | ||
xRoadClient: string | ||
userAuth?: any | ||
fetch?: Partial<EnhancedFetchOptions> | ||
} | ||
|
||
export class AssetsClientModule { | ||
static register(config: ModuleConfig): DynamicModule { | ||
const headers = { | ||
'X-Road-Client': config.xRoadClient, | ||
Accept: 'application/json', | ||
} | ||
const providerConfiguration = new Configuration({ | ||
fetchApi: createEnhancedFetch({ | ||
name: 'clients-assets', | ||
...config.fetch, | ||
}), | ||
basePath: config.xRoadPath, | ||
headers, | ||
}) | ||
|
||
const exportedApis = [FasteignirApi] | ||
|
||
return { | ||
module: AssetsClientModule, | ||
providers: exportedApis.map((Api) => ({ | ||
provide: Api, | ||
useFactory: () => new Api(providerConfiguration), | ||
})), | ||
exports: exportedApis, | ||
} | ||
} | ||
} | ||
import { Module } from '@nestjs/common' | ||
import { PropertiesApiProvider } from './PropertiesApiProvider' | ||
|
||
@Module({ | ||
providers: [PropertiesApiProvider], | ||
exports: [PropertiesApiProvider], | ||
}) | ||
export class AssetsClientModule {} |
Oops, something went wrong.