Skip to content
Navigation Menu
{{ message }}
forked from GoogleCloudPlatform/php-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkms.php
More file actions
326 lines (276 loc) · 12.9 KB
/
Copy pathkms.php
File metadata and controls
326 lines (276 loc) · 12.9 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Samples\Kms;
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventDispatcher;
$application = new Application('Google Cloud Key Management Store (KMS)');
$application->setDispatcher($dispatcher = new EventDispatcher());
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
$input = $event->getInput();
// Try to get the default project ID from gcloud
if ($input->hasOption('project') && !$input->getOption('project')) {
exec(
"gcloud config list --format 'value(core.project)' 2>/dev/null",
$output,
$return_var
);
if (0 !== $return_var) {
throw new \Exception('Could not derive a project ID from gcloud. ' .
'You must supply a project ID using --project');
}
$input->setOption('project', array_pop($output));
}
});
$inputDefinition = new InputDefinition([
new InputOption(
'project',
'p',
InputOption::VALUE_REQUIRED,
'The Google Cloud Platform project name to use for this invocation. ' .
'If omitted then the current gcloud project is assumed. '
),
new InputOption(
'location',
null,
InputOption::VALUE_REQUIRED,
'The location of the cryptokey or keyring.',
'global'
),
]);
// Add Encryption Command
$application->add((new Command('encryption'))
->setDescription('Manage encryption for KMS')
->setDefinition(clone $inputDefinition)
->addArgument('keyring', InputArgument::REQUIRED, 'The name of the keyring.')
->addArgument('cryptokey', InputArgument::REQUIRED, 'The name of the cryptokey.')
->addArgument('infile', InputArgument::REQUIRED, 'The target file.')
->addArgument('outfile', InputArgument::REQUIRED, 'The file to store the result.')
->addOption('decrypt', null, InputOption::VALUE_NONE, 'Performs the decrypt function instead of encrypt. ')
->setHelp(<<<EOF
The <info>%command.name%</info> command uses the KMS API to encrypt and decrypt text in files.
Encrypt the text of a file using the specified CryptoKey:
<info>php %command.full_name% my-keyring my-cryptokey file.txt file.txt.encrypted</info>
Decrypt the text of a file using the specified CryptoKey:
<info>php %command.full_name% my-keyring my-cryptokey file.txt.encrypted file.txt.decrypted --decrypt</info>
EOF
)
->setCode(function ($input, $output) {
$projectId = $input->getOption('project');
$keyRing = $input->getArgument('keyring');
$cryptoKey = $input->getArgument('cryptokey');
$infile = $input->getArgument('infile');
$outfile = $input->getArgument('outfile');
$location = $input->getOption('location');
if ($input->getOption('decrypt')) {
decrypt($projectId, $keyRing, $cryptoKey, $infile, $outfile, $location);
} else {
encrypt($projectId, $keyRing, $cryptoKey, $infile, $outfile, $location);
}
})
);
// Add IAM Command
$application->add((new Command('iam'))
->setDescription('Manage IAM for KMS')
->setDefinition(clone $inputDefinition)
->addArgument('keyring', InputArgument::REQUIRED, 'The name of the keyring.')
->addArgument('cryptokey', InputArgument::OPTIONAL, 'The name of the cryptokey.')
->addOption('service-account-email', null, InputOption::VALUE_REQUIRED, 'The service accunt email to add to the policy.')
->addOption('user-email', null, InputOption::VALUE_REQUIRED, 'The user email to add to the policy.')
->addOption('role', null, InputOption::VALUE_REQUIRED, 'The role of the policy.', 'roles/cloudkms.cryptoKeyEncrypterDecrypter')
->addOption('remove', null, InputOption::VALUE_NONE, 'If supplied, will remove the user or service account from the policy')
->setHelp(<<<EOF
The <info>%command.name%</info> command manages KMS IAM policies.
List the IAM roles for a KeyRing:
<info>php %command.full_name% my-keyring</info>
List the IAM roles for a CryptoKey:
<info>php %command.full_name% my-keyring my-cryptokey</info>
Add a service account to a CryptoKey:
<info>php %command.full_name% my-keyring my-cryptokey \
--service-account-email=example@my-project.gserviceaccount.com</info>
Add a service account to a CryptoKey for a specific role:
<info>php %command.full_name% my-keyring my-cryptokey \
--service-account-email=example@my-project.gserviceaccount.com \
--role=roles/cloudkms.admin</info>
EOF
)
->setCode(function ($input, $output) {
$projectId = $input->getOption('project');
$keyRing = $input->getArgument('keyring');
$cryptoKey = $input->getArgument('cryptokey');
$role = $input->getOption('role');
$location = $input->getOption('location');
$userEmail = $input->getOption('user-email');
$serviceAccountEmail = $input->getOption('service-account-email');
if ($cryptoKey) {
if (empty($userEmail) && empty($serviceAccountEmail)) {
get_cryptokey_policy($projectId, $keyRing, $cryptoKey, $location);
} else {
if ($userEmail) {
$member = 'user:' . $userEmail;
} else {
$member = 'serviceAccount:' . $serviceAccountEmail;
}
if ($input->getOption('remove')) {
remove_member_from_cryptokey_policy($projectId, $keyRing, $cryptoKey, $member, $role, $location);
} else {
add_member_to_cryptokey_policy($projectId, $keyRing, $cryptoKey, $member, $role, $location);
}
}
} else {
if (empty($userEmail) && empty($serviceAccountEmail)) {
get_keyring_policy($projectId, $keyRing, $location);
} else {
if ($userEmail) {
$member = 'user:' . $userEmail;
} else {
$member = 'serviceAccount:' . $serviceAccountEmail;
}
if ($input->getOption('remove')) {
remove_member_from_keyring_policy($projectId, $keyRing, $member, $role, $location);
} else {
add_member_to_keyring_policy($projectId, $keyRing, $member, $role, $location);
}
}
}
})
);
// Add Key Command
$application->add((new Command('key'))
->setDescription('Manage keys for KMS')
->setDefinition(clone $inputDefinition)
->addArgument('keyring', InputArgument::REQUIRED, 'The name of the keyring.')
->addArgument('cryptokey', InputArgument::OPTIONAL, 'The name of the cryptokey.')
->addOption('create', null, InputOption::VALUE_NONE, 'If supplied, will create the keyring, cryptokey, or cryptokey version')
->setHelp(<<<EOF
The <info>%command.name%</info> command manages KMS keys.
List all CrytoKeys for the supplied KeyRing:
<info>php %command.full_name% my-keyring</info>
Display information about a CrytoKey:
<info>php %command.full_name% my-keyring my-cryptokey</info>
Create a CrytoKey:
<info>php %command.full_name% my-keyring new-cryptokey --create</info>
EOF
)
->setCode(function ($input, $output) {
$projectId = $input->getOption('project');
$keyRing = $input->getArgument('keyring');
$cryptoKey = $input->getArgument('cryptokey');
$location = $input->getOption('location');
if ($cryptoKey) {
if ($input->getOption('create')) {
create_cryptokey($projectId, $keyRing, $cryptoKey, $location);
} else {
get_cryptokey($projectId, $keyRing, $cryptoKey, $location);
}
} else {
list_cryptokeys($projectId, $keyRing, $location);
}
})
);
// Add KeyRing Command
$application->add((new Command('keyring'))
->setDescription('Manage keyrings for KMS')
->setDefinition(clone $inputDefinition)
->addArgument('keyring', InputArgument::OPTIONAL, 'The name of the keyring.')
->addOption('create', null, InputOption::VALUE_NONE, 'If supplied, will create the keyring, cryptokey, or cryptokey version')
->setHelp(<<<EOF
The <info>%command.name%</info> command manages KMS keyrings.
List all KeyRings for a project:
<info>php %command.full_name%</info>
Display information about a KeyRing:
<info>php %command.full_name% my-keyring</info>
Create a KeyRing:
<info>php %command.full_name% new-keyring --create</info>
EOF
)
->setCode(function ($input, $output) {
$projectId = $input->getOption('project');
$ring = $input->getArgument('keyring');
$location = $input->getOption('location');
if ($ring) {
if ($input->getOption('create')) {
create_keyring($projectId, $ring, $location);
} else {
get_keyring($projectId, $ring, $location);
}
} else {
list_keyrings($projectId, $location);
}
})
);
// Add Version Command
$application->add((new Command('version'))
->setDescription('Manage key versions for KMS')
->setDefinition(clone $inputDefinition)
->addArgument('keyring', InputArgument::REQUIRED, 'The name of the keyring.')
->addArgument('cryptokey', InputArgument::REQUIRED, 'The name of the cryptokey.')
->addArgument('version', InputArgument::OPTIONAL, 'The version of the cryptokey.')
->addOption('create', null, InputOption::VALUE_NONE, 'If supplied, will create the keyring, cryptokey, or cryptokey version')
->addOption('destroy', null, InputOption::VALUE_NONE, 'If supplied, will destroy the cryptokey version')
->addOption('disable', null, InputOption::VALUE_NONE, 'If supplied, will disable the cryptokey version')
->addOption('enable', null, InputOption::VALUE_NONE, 'If supplied, will enable the cryptokey version')
->addOption('restore', null, InputOption::VALUE_NONE, 'If supplied, will restore the cryptokey version')
->addOption('set-primary', null, InputOption::VALUE_NONE, 'If supplied, will disable the cryptokey version')
->setHelp(<<<EOF
The <info>%command.name%</info> command manages KMS key versions.
List all versions of a CryptoKey:
<info>php %command.full_name% my-keyring my-cryptokey</info>
Display information about a specific CryptoKey version:
<info>php %command.full_name% my-keyring my-cryptokey 1</info>
Create a new CryptoKey version:
<info>php %command.full_name% my-keyring my-cryptokey --create</info>
EOF
)
->setCode(function ($input, $output) {
$projectId = $input->getOption('project');
$keyRing = $input->getArgument('keyring');
$cryptoKey = $input->getArgument('cryptokey');
$cryptoKeyVersion = $input->getArgument('version');
$location = $input->getOption('location');
if ($input->getOption('create')) {
create_cryptokey_version($projectId, $keyRing, $cryptoKey, $location);
} elseif ($cryptoKeyVersion) {
if ($input->getOption('destroy')) {
destroy_cryptokey_version($projectId, $keyRing, $cryptoKey, $cryptoKeyVersion, $location);
} elseif ($input->getOption('disable')) {
disable_cryptokey_version($projectId, $keyRing, $cryptoKey, $cryptoKeyVersion, $location);
} elseif ($input->getOption('restore')) {
restore_cryptokey_version($projectId, $keyRing, $cryptoKey, $cryptoKeyVersion, $location);
} elseif ($input->getOption('enable')) {
enable_cryptokey_version($projectId, $keyRing, $cryptoKey, $cryptoKeyVersion, $location);
} elseif ($input->getOption('set-primary')) {
set_cryptokey_primary_version($projectId, $keyRing, $cryptoKey, $cryptoKeyVersion, $location);
} else {
get_cryptokey_version($projectId, $keyRing, $cryptoKey, $cryptoKeyVersion, $location);
}
} else {
list_cryptokey_versions($projectId, $keyRing, $cryptoKey, $location);
}
})
);
// for testing
if (getenv('PHPUNIT_TESTS') === '1') {
return $application;
}
$application->run();
You can’t perform that action at this time.
