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

Add retry data store sync on armory boot #354

Merged
merged 15 commits into from
Jun 21, 2024
4 changes: 2 additions & 2 deletions apps/devtool/src/app/_hooks/useEngineApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ const useEngineApi = () => {
try {
setErrors(undefined)
setIsProcessing(true)
const { latestSync } = await syncPolicyEngine(sdkEngineClientConfig)
setIsSynced(latestSync.success)
const { success } = await syncPolicyEngine(sdkEngineClientConfig)
setIsSynced(success)
setTimeout(() => setIsSynced(false), 5000)
} catch (error) {
setErrors(extractErrorMessage(error))
Expand Down
2 changes: 0 additions & 2 deletions apps/policy-engine/src/engine/engine.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ConfigService } from '@narval/config-module'
import { EncryptionModule } from '@narval/encryption-module'
import { HttpModule } from '@nestjs/axios'
import { Module, ValidationPipe } from '@nestjs/common'
import { APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core'
import { ZodSerializerInterceptor, ZodValidationPipe } from 'nestjs-zod'
Expand Down Expand Up @@ -28,7 +27,6 @@ import { HttpDataStoreRepository } from './persistence/repository/http-data-stor

@Module({
imports: [
HttpModule,
KeyValueModule,
EncryptionModule.registerAsync({
imports: [EngineModule],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EntityData, FIXTURE, HttpSource, SourceType } from '@narval/policy-engine-shared'
import { HttpModule } from '@nestjs/axios'
import { HttpStatus } from '@nestjs/common'
import { Test } from '@nestjs/testing'
import nock from 'nock'
Expand All @@ -24,7 +23,6 @@ describe(HttpDataStoreRepository.name, () => {

beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [HttpModule],
providers: [HttpDataStoreRepository]
}).compile()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { HttpSource } from '@narval/policy-engine-shared'
import { HttpService } from '@nestjs/axios'
import { HttpStatus, Injectable } from '@nestjs/common'
import { catchError, lastValueFrom, map } from 'rxjs'
import axios from 'axios'
import axiosRetry from 'axios-retry'

Check failure on line 4 in apps/policy-engine/src/engine/persistence/repository/http-data-store.repository.ts

View workflow job for this annotation

GitHub Actions / Test

Cannot find module 'axios-retry' or its corresponding type declarations.
import { DataStoreException } from '../../core/exception/data-store.exception'
import { DataStoreRepository } from '../../core/repository/data-store.repository'

const MAX_RETRIES = 3

@Injectable()
export class HttpDataStoreRepository implements DataStoreRepository {
constructor(private httpService: HttpService) {}
async fetch<Data>(source: HttpSource): Promise<Data> {
try {
const client = axios.create()
axiosRetry(client, { retries: MAX_RETRIES, retryDelay: axiosRetry.exponentialDelay })
samteb marked this conversation as resolved.
Show resolved Hide resolved
const { data } = await client.get<Data>(source.url, { headers: source.headers })

fetch<Data>(source: HttpSource): Promise<Data> {
return lastValueFrom(
this.httpService.get<Data>(source.url, { headers: source.headers }).pipe(
map((response) => response.data),
catchError((error) => {
throw new DataStoreException({
message: 'Unable to fetch remote data source via HTTP',
suggestedHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
context: { source },
origin: error
})
})
)
)
return data
} catch (error) {
throw new DataStoreException({
message: 'Unable to fetch remote data source via HTTP',
suggestedHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
context: { source },
origin: error
})
}
}
}
4 changes: 2 additions & 2 deletions packages/armory-sdk/src/lib/http/policy-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ export const sendEvaluationRequest = async (
}
}

export const syncPolicyEngine = async (config: EngineClientConfig): Promise<{ latestSync: { success: boolean } }> => {
export const syncPolicyEngine = async (config: EngineClientConfig): Promise<{ success: boolean }> => {
try {
const { engineHost, engineClientId: clientId, engineClientSecret: clientSecret } = config

if (!clientSecret) {
throw new ArmorySdkException('Client secret is required to sync engine', { config })
}

const { data } = await axios.post<{ latestSync: { success: boolean } }>(`${engineHost}/clients/sync`, null, {
const { data } = await axios.post<{ success: boolean }>(`${engineHost}/clients/sync`, null, {
headers: builBasicHeaders({ clientId, clientSecret })
})

Expand Down
Loading