Skip to content

Commit 05be30a

Browse files
committed
Merge branch 'feature/CG-662' into 'master'
feat(plugins): Rework plugins config block. Closes CG-662 See merge request auto-cloud/cloudgraph/cli!192
2 parents a6b6300 + e124060 commit 05be30a

7 files changed

Lines changed: 145 additions & 68 deletions

File tree

.cloud-graphrc.example.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
},
2929
"versionLimit": 10,
3030
"queryEngine": "playground",
31-
"port": "5555"
31+
"port": "5555",
32+
"plugins": {
33+
"policyPack": [
34+
{
35+
"name": "aws-cis-1.2",
36+
"providers": ["aws"]
37+
}
38+
]
39+
}
3240
}
33-
}
41+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"bugs": "https://github.com/cloudgraphdev/cli/issues",
1717
"dependencies": {
18-
"@cloudgraph/sdk": "^0.12.0",
18+
"@cloudgraph/sdk": "^0.13.0",
1919
"@graphql-tools/load-files": "^6.3.2",
2020
"@graphql-tools/merge": "^8.2.0",
2121
"@oclif/command": "^1",

src/commands/init.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { StorageEngineConnectionConfig } from '@cloudgraph/sdk'
1+
import {
2+
ConfiguredPlugin,
3+
StorageEngineConnectionConfig,
4+
} from '@cloudgraph/sdk'
25
import { flags as CommandFlags } from '@oclif/command'
36
import fs from 'fs'
47
import path from 'path'
@@ -65,6 +68,15 @@ export default class Init extends Command {
6568
return client.configure()
6669
}
6770

71+
getPluginConfig(): {
72+
plugins: { [pluginType: string]: ConfiguredPlugin[] }
73+
} {
74+
const plugins = this.getCGConfigKey('plugins')
75+
return {
76+
plugins: plugins ?? {},
77+
}
78+
}
79+
6880
async askForDGraphConfig(overwrite = false): Promise<{
6981
versionLimit: string
7082
storageConfig: StorageEngineConnectionConfig
@@ -121,6 +133,7 @@ export default class Init extends Command {
121133

122134
async getCloudGraphConfig(overwrite = false): Promise<CloudGraphConfig> {
123135
return {
136+
...this.getPluginConfig(),
124137
...(await this.askForDGraphConfig(overwrite)),
125138
...(await this.askForQueryEngineConfig(overwrite)),
126139
}

src/commands/policy/add.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PluginType } from '@cloudgraph/sdk'
2+
import { isEmpty, uniqBy } from 'lodash'
23

34
import OperationBaseCommand from '../operation'
45

@@ -28,10 +29,32 @@ export default class AddPolicy extends OperationBaseCommand {
2829

2930
// Save policy to CG config file
3031
const config = this.getCGConfig()
31-
if (config && config[provider]) {
32-
config[provider].policies = config[provider].policies
33-
? [...new Set([...config[provider].policies, key])]
34-
: [key]
32+
if (config) {
33+
let configuredPolicies =
34+
config.cloudGraph.plugins[PluginType.PolicyPack] || []
35+
36+
if (isEmpty(configuredPolicies)) {
37+
// Set new Policy Pack Plugin array
38+
configuredPolicies = [
39+
{
40+
name: key,
41+
providers: [provider],
42+
},
43+
]
44+
} else {
45+
// Add policy to Policy Pack Plugin array
46+
configuredPolicies = [
47+
...configuredPolicies,
48+
{
49+
name: key,
50+
providers: [provider],
51+
},
52+
]
53+
}
54+
config.cloudGraph.plugins[PluginType.PolicyPack] = uniqBy(
55+
configuredPolicies,
56+
'name'
57+
)
3558
this.saveCloudGraphConfigFile(config)
3659
}
3760
}

src/commands/policy/remove.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { PluginType } from '@cloudgraph/sdk'
1+
import { ConfiguredPlugin, PluginType } from '@cloudgraph/sdk'
2+
import isEmpty from 'lodash/isEmpty'
23

34
import OperationBaseCommand from '../operation'
45

@@ -36,16 +37,20 @@ export default class RemovePolicy extends OperationBaseCommand {
3637
if (manager && !noSave) {
3738
manager.removeFromLockFile(key)
3839

39-
const [provider] = key.split('-')
4040
const config = this.getCGConfig()
41-
42-
if (config[provider]) {
43-
config[provider].policies = [
44-
...config[provider].policies.filter(
45-
(policy: string) => policy !== key
46-
),
47-
]
48-
this.saveCloudGraphConfigFile(config)
41+
if (config) {
42+
const configuredPolicies =
43+
config.cloudGraph.plugins[PluginType.PolicyPack] || []
44+
45+
if (!isEmpty(configuredPolicies)) {
46+
// Remove policy from Policy Pack Plugin array
47+
config.cloudGraph.plugins[PluginType.PolicyPack] =
48+
configuredPolicies.filter(
49+
(p: ConfiguredPlugin) => p.name !== key
50+
)
51+
52+
this.saveCloudGraphConfigFile(config)
53+
}
4954
}
5055
}
5156
}

src/commands/scan.ts

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from 'chalk'
22
import fs from 'fs'
33
import path from 'path'
4-
import { cloudGraphPlugin, Opts, pluginMap } from '@cloudgraph/sdk'
4+
import { Opts, pluginMap, PluginType, StorageEngine } from '@cloudgraph/sdk'
55
import { range } from 'lodash'
66

77
import Command from './base'
@@ -31,6 +31,66 @@ export default class Scan extends Command {
3131

3232
static args = Command.args
3333

34+
private async plugins({
35+
storage: { isRunning, engine },
36+
flags,
37+
}: {
38+
storage: {
39+
isRunning: boolean
40+
engine: StorageEngine
41+
}
42+
flags: {
43+
[flag: string]: any
44+
}
45+
}): Promise<void> {
46+
const config = this.getCGConfig('cloudGraph')
47+
const { plugins = {} } = config
48+
for (const pluginType in plugins) {
49+
if (pluginType) {
50+
try {
51+
// Get Plugin Interface
52+
const Plugin = pluginMap[pluginType]
53+
54+
// Execute Plugins by Provider
55+
for (const provider in this.providers) {
56+
if (provider) {
57+
const { schemasMap, serviceKey } = this.providers[provider]
58+
59+
// Initialize
60+
const PluginInstance = new Plugin({
61+
config,
62+
provider: {
63+
name: provider,
64+
schemasMap,
65+
serviceKey,
66+
},
67+
flags: flags as { [flag: string]: any },
68+
logger: this.logger,
69+
})
70+
71+
// Get the Plugin Manager
72+
const pluginManager = this.getPluginManager(
73+
pluginType as PluginType
74+
)
75+
76+
// Configure
77+
await PluginInstance.configure(pluginManager, plugins[pluginType])
78+
79+
// Execute plugins
80+
await PluginInstance.execute({
81+
storageRunning: isRunning,
82+
storageEngine: engine,
83+
processConnectionsBetweenEntities,
84+
})
85+
}
86+
}
87+
} catch (error) {
88+
this.logger.warn('Plugin not supported by CG')
89+
}
90+
}
91+
}
92+
}
93+
3494
async run() {
3595
const { argv, flags } = this.parse(Scan)
3696
const { dev: devMode } = flags as {
@@ -39,7 +99,6 @@ export default class Scan extends Command {
3999

40100
const { dataDir } = this.config
41101
const opts: Opts = { logger: this.logger, debug: true, devMode }
42-
const configuredPlugins = []
43102
let allProviders = argv
44103

45104
// Run dgraph health check
@@ -112,49 +171,18 @@ export default class Scan extends Command {
112171
this.logger.info(
113172
`Beginning ${chalk.italic.green('SCAN')} for ${provider}`
114173
)
115-
const { client: providerClient, schemasMap, serviceKey } =
116-
await this.getProviderClient(provider)
174+
175+
const providerPlugin = await this.getProviderClient(provider)
176+
const { client: providerClient, schemasMap } = providerPlugin
177+
117178
if (!providerClient) {
118179
failedProviderList.push(provider)
119180
this.logger.warn(`No valid client found for ${provider}, skipping...`)
120181
continue // eslint-disable-line no-continue
121182
}
122183
const config = this.getCGConfig(provider)
184+
this.providers[provider] = providerPlugin
123185

124-
// Configure installed plugins
125-
for (const key in config) {
126-
if (cloudGraphPlugin[key]) {
127-
try {
128-
// Get Plugin Interface
129-
const Plugin = pluginMap[cloudGraphPlugin[key]]
130-
131-
// Initialize
132-
const PluginInstance = new Plugin({
133-
config,
134-
provider: {
135-
name: provider,
136-
schemasMap,
137-
serviceKey,
138-
},
139-
flags: flags as { [flag: string]: any },
140-
logger: this.logger,
141-
})
142-
143-
// Get the Plugin Manager
144-
const pluginManager = this.getPluginManager(
145-
cloudGraphPlugin[key]
146-
)
147-
148-
// Configure
149-
await PluginInstance.configure(pluginManager)
150-
151-
// Add to Configured Plugins list
152-
configuredPlugins.push(PluginInstance)
153-
} catch (error) {
154-
this.logger.warn('Plugin not supported by CG')
155-
}
156-
}
157-
}
158186
this.logger.debug(config)
159187
if (!config) {
160188
failedProviderList.push(provider)
@@ -263,15 +291,15 @@ export default class Scan extends Command {
263291

264292
this.logger.successSpinner('Data insertion into Dgraph complete')
265293

266-
// Execute plugins
267-
for (const plugin of configuredPlugins) {
268-
await plugin.execute({
269-
storageRunning,
270-
storageEngine,
271-
processConnectionsBetweenEntities,
272-
})
273-
}
294+
await this.plugins({
295+
flags: flags as { [flag: string]: any },
296+
storage: {
297+
isRunning: storageRunning,
298+
engine: storageEngine,
299+
},
300+
})
274301
}
302+
275303
scanReport.print()
276304

277305
this.logger.success(

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,10 @@
379379
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
380380
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
381381

382-
"@cloudgraph/sdk@^0.12.0":
383-
version "0.12.0"
384-
resolved "https://registry.yarnpkg.com/@cloudgraph/sdk/-/sdk-0.12.0.tgz#c95b87bed8883bde072c5e4cf171b4ef50d606dc"
385-
integrity sha512-RD1HYwHlWKuyekdaLLOhfAATRaKmkHpWjU5vM6kEtiUGhGzR2ywujmIza7vbYCi6o23gLA38nAfaGi9VA239zg==
382+
"@cloudgraph/sdk@^0.13.0":
383+
version "0.13.0"
384+
resolved "https://registry.yarnpkg.com/@cloudgraph/sdk/-/sdk-0.13.0.tgz#1c093621d71cb8c10e6e638f094e8ddc01dee315"
385+
integrity sha512-rqCcBuobib8cyKThxJQf4Wo0l6Thio2sNeP/AtR+EtSd4dxBTTBbTxQnuFkDLk4EJdgge2095nBvXTNeZH4Dtg==
386386
dependencies:
387387
"@graphql-tools/load-files" "^6.5.3"
388388
"@graphql-tools/merge" "^8.2.1"

0 commit comments

Comments
 (0)