forked from Arinono/clean_secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (131 loc) · 3.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { SecretManagerServiceClient } from '@google-cloud/secret-manager'
import fs from 'fs'
import readline from 'readline'
class MissingEnv extends Error {
constructor(name) {
super(`You must provide ${name} as an environement variable`)
this.name = MissingEnv.name
}
}
/**
*
* @param {string} name
* @returns {string}
*/
const envOrThrow = name => {
const res = process.env[name]
if (res) {
return res
}
throw new MissingEnv(name)
}
/**
* @returns {string[]}
*/
const getSkips = () => {
if (!fs.existsSync('./skip.txt')) {
return []
}
const skips = []
const raw = fs.readFileSync('./skip.txt').toString('utf-8')
for (const l of raw.split('\n')) {
if (l.length === 0) {
continue
}
skips.push(l.trim())
}
return skips
}
/**
*
* @param {google.cloud.secretmanager.v1.ISecret[]} skipped
* @returns {string}
*/
const displaySkipped = (skipped) => {
let str = ''
for (const s of skipped) {
const [_projects, _projectsId, _secrets, name] = s.name.split('/')
if (!name) {
continue
}
str += `- ${name}\n`
}
return str
}
/**
*
* @param {google.cloud.secretmanager.v1.ISecret[]} secrets
* @param {string[]} skips
* @returns {google.cloud.secretmanager.v1.ISecret[]}
*/
const whatToDelete = (secrets, skips) => {
const toDelete = []
const skipped = []
for (const s of secrets) {
const [_projects, _projectsId, _secrets, name] = s.name.split('/')
if (!name) {
continue
}
if (skips.find(n => n === name)) {
skipped.push(s)
} else {
toDelete.push(s)
}
}
return { toDelete, skipped }
}
/**
*
* @param {SecretManagerServiceClient} client
* @param {google.cloud.secretmanager.v1.ISecret[]} secrets
* @returns {Promise<void>}
*/
const deleteSecrets = async (client, secrets) => {
for (const { name } of secrets) {
if (process.env['DRY_RUN'] === 'true') {
console.log(`DRY_RUN: ${name} deleted.`)
} else {
await client.deleteSecret({ name })
console.log(`${name} deleted.`)
}
}
}
envOrThrow('GOOGLE_APPLICATION_CREDENTIALS')
const project = envOrThrow('PROJECT')
const client = new SecretManagerServiceClient()
const [secrets] = await client.listSecrets({
parent: project
})
const skips = getSkips()
const { toDelete, skipped } = whatToDelete(secrets, skips)
console.log(`
I will be deleting ${toDelete.length} secrets out of ${secrets.length}.
Here are the ones that I'll skip. You can add skips in the 'skip.txt'.
${displaySkipped(skipped)}
`)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question('Do you want to proceed? [y/n] ', fstyn => {
if (fstyn === 'y') {
rl.question('Are you sure? [y/n] ', yn => {
if (yn === 'y') {
console.time('Done in')
deleteSecrets(client, toDelete).then(() => {
rl.close()
console.timeEnd('Done in')
process.exit(0)
})
} else {
rl.close()
console.log('\nIf you\'re scared, you can use DRY_RUN\n')
process.exit(1)
}
})
} else {
rl.close()
console.log('\nIf you\'re scared, you can use DRY_RUN\n')
process.exit(1)
}
})