Skip to content

Commit

Permalink
chore: Integrated the anomaly detection plugin with the resource-reso…
Browse files Browse the repository at this point in the history
…lver
  • Loading branch information
zoemaas committed Nov 7, 2024
1 parent d85db50 commit 53e5abd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/resource-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@sphereon/ssi-sdk.kv-store-temp": "workspace:*",
"@sphereon/ssi-sdk.anomaly-detection": "workspace:*",
"cross-fetch": "^3.1.8",
"debug": "^4.3.5",
"typeorm": "^0.3.20",
Expand Down
27 changes: 25 additions & 2 deletions packages/resource-resolver/src/agent/ResourceResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ export class ResourceResolver implements IAgentPlugin {
private readonly defaultStoreId: string
private readonly defaultNamespace: string
private readonly defaultTtl: number
private readonly detectLocation: boolean
private readonly _resourceStores: Map<string, IKeyValueStore<Resource>>

constructor(options?: ResourceResolverOptions) {
const {
defaultStore,
defaultNamespace,
resourceStores,
ttl
ttl,
detectLocation
} = options ?? {}

this.defaultStoreId = defaultStore ?? '_default'
this.defaultNamespace = defaultNamespace ?? 'resources'
this.defaultTtl = ttl ?? 3600
this.detectLocation = detectLocation ?? false

if (resourceStores && resourceStores instanceof Map) {
this._resourceStores = resourceStores
Expand Down Expand Up @@ -101,10 +104,16 @@ export class ResourceResolver implements IAgentPlugin {
});
}

let location
if (this.detectLocation) {
location = await this.retrieveLocation(input, context)
}

const response = await fetch(input, init)
if (!resolveOpts?.skipPersistence && (response.status >= 200 && response.status < 300)) {
const serializedResponse = await serializeResponse(response);
const resource = {
const resource: Resource = {
location,
response: serializedResponse,
resourceType,
insertedAt: Date.now(),
Expand All @@ -127,6 +136,20 @@ export class ResourceResolver implements IAgentPlugin {
return response
}

private async retrieveLocation(input: RequestInfo | URL, context: RequiredContext) {
let url: URL
if (input instanceof Request && input.url !== undefined && input.url !== null) {
url = new URL(input.url)
} else if (input instanceof URL) {
url = input
} else {
throw Error(`input type is required to be RequestInfo | URL`)
}
return await context.agent.lookupLocation({
ipOrHostname: url.hostname
})
}

/** {@inheritDoc IResourceResolver.resourceClearAllResources} */
private async resourceClearAllResources(args: ClearAllResourcesArgs, context: RequiredContext): Promise<boolean> {
const { storeId } = args
Expand Down
14 changes: 11 additions & 3 deletions packages/resource-resolver/src/types/IResourceResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IKeyValueStore, ValueStoreType } from '@sphereon/ssi-sdk.kv-store-temp'
import { IAgentContext, IPluginMethodMap } from '@veramo/core'
import {IKeyValueStore, ValueStoreType} from '@sphereon/ssi-sdk.kv-store-temp'
import {IAgentContext, IPluginMethodMap} from '@veramo/core'
import {IAnomalyDetection} from "@sphereon/ssi-sdk.anomaly-detection";

export interface IResourceResolver extends IPluginMethodMap {
resourceResolve(args: ResolveArgs, context: RequiredContext): Promise<Response>
Expand All @@ -14,6 +15,7 @@ export type ResourceResolverOptions = {
defaultNamespace?: string
resourceStores?: Map<string, IKeyValueStore<Resource>> | IKeyValueStore<Resource>
ttl?: number
detectLocation?: boolean
}

export type ResolveArgs = {
Expand Down Expand Up @@ -71,7 +73,13 @@ export type StoreArgs<T extends ValueStoreType> = {
storeId?: string
}

export type Location = {
continent?: string
country?: string
}

export type Resource = {
location?: Location | null
response: SerializedResponse
resourceType: ResourceType
insertedAt: number
Expand All @@ -85,4 +93,4 @@ export type SerializedResponse = {
body: string
}

export type RequiredContext = IAgentContext<never>
export type RequiredContext = IAgentContext<IAnomalyDetection>
3 changes: 3 additions & 0 deletions packages/resource-resolver/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
{
"path": "../agent-config"
},
{
"path": "../anomaly-detection"
},
{
"path": "../kv-store"
}
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 53e5abd

Please sign in to comment.