Skip to content

Commit

Permalink
feat(cli): add command to verify an agent configuration file (#729)
Browse files Browse the repository at this point in the history
Run `veramo config check` in a terminal to do a simple syntax check on the agent.yml file
  • Loading branch information
mirceanis committed Oct 27, 2021
1 parent 801bb95 commit 2790ebc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
5 changes: 3 additions & 2 deletions packages/cli/default/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ constants:
baseUrl: http://localhost:3332
port: 3332
# please use your own X25519 key, this is only an example
secretKey: 29739248cad1bd1a0fc4d9b75cd4d2990de535baf5caadfdf8d8f86664aa830c
# you can generate a new key by running `veramo config gen-key` in a terminal
dbEncryptionKey: 29739248cad1bd1a0fc4d9b75cd4d2990de535baf5caadfdf8d8f86664aa830c
databaseFile: ./database.sqlite
methods:
- keyManagerGetKeyManagementSystems
Expand Down Expand Up @@ -206,7 +207,7 @@ keyManager:
- $ref: /dbConnection
- $require: '@veramo/kms-local#SecretBox'
$args:
- $ref: /constants/secretKey
- $ref: /constants/dbEncryptionKey

# DID Manager
didManager:
Expand Down
42 changes: 39 additions & 3 deletions packages/cli/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'cross-fetch/polyfill'
import program from 'commander'
import { SecretBox } from '@veramo/kms-local'
import { getAgent, getConfig } from './setup'
const fs = require('fs')
const { dirname } = require('path')

Expand Down Expand Up @@ -38,12 +39,47 @@ config

config
.command('create-secret-key')
.alias('gen-key')
.alias('key-gen')
.description('generate secret key')
.action(async (raw) => {
.option('-q, --quiet', 'Only print the raw key, no instructions', false)
.action(async (options) => {
try {
const secretKey = await SecretBox.createSecretKey()
console.log(secretKey)
const dbEncryptionKey = await SecretBox.createSecretKey()
if (options.quiet === true) {
console.log(dbEncryptionKey)
} else {
console.log(`
X25519 raw private key (hex encoded):
${dbEncryptionKey}
You can use this key with @veramo/kms-local#SecretBox
or replace the default agent.yml 'dbEncryptionKey' constant
`)
}
} catch (e: any) {
console.error(e.message)
}
})

config
.command('check')
.alias('verify')
.description('Verify an agent config file syntax')
.option('-f, --filename <string>', 'Config file name', './agent.yml')
.option('-m, --method <string>', 'Check that a specific method is exposed by the agent.', 'execute')
.action(async (options) => {
const agent = getAgent(options.filename)
if (!agent) {
console.error(
'unknown error while creating the agent from your config. Consider running `veramo config create` to generate a new configuration file, or to manually compare differences.',
)
} else {
if (typeof agent[options.method] !== 'function') {
console.error(`The agent was created using the config, but the 'agent.${options.method}()' method is not available. Make sure the plugin that implements that method is installed.`)
} else {
console.log(`Your Veramo configuration seems fine. An agent can be created and the 'agent.${options.method}()' method can be called on it.`)
}
}
})
19 changes: 7 additions & 12 deletions packages/cli/src/dev.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { getAgent } from './setup'
import program from 'commander'
const fs = require('fs')
import { resolve, dirname } from 'path'
import { writeFileSync, readFileSync } from 'fs'
import * as TJS from 'ts-json-schema-generator'
import { JSONSchema7 } from 'json-schema'
import { OpenAPIV3 } from 'openapi-types'
import { Extractor, ExtractorConfig, ExtractorResult } from '@microsoft/api-extractor'
import {
ApiMethodSignature,
ApiModel,
ApiPackage,
ApiParameterListMixin,
ApiDocumentedItem,
ApiReturnTypeMixin,
ApiMethodSignature,
} from '@microsoft/api-extractor-model'
import { IIdentifier } from '@veramo/core'
import program from 'commander'
import { writeFileSync } from 'fs'
import { OpenAPIV3 } from 'openapi-types'
import { resolve } from 'path'
import * as TJS from 'ts-json-schema-generator'
const fs = require('fs')

interface Method {
packageName: string
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getConfig = (fileName: string): any => {
process.exit(1)
}

const config = yaml.parse(fs.readFileSync(fileName).toString())
const config = yaml.parse(fs.readFileSync(fileName).toString(), { prettyErrors: true })

if (config?.version != 3) {
console.log('Unsupported configuration file version:', config.version)
Expand Down

0 comments on commit 2790ebc

Please sign in to comment.