Skip to content

Commit 14190a5

Browse files
authored
Merge pull request #871 from oracle-devrel/ios-kms-backup-14022024
Ios kms backup 14022024
2 parents 62b7065 + 173fa90 commit 14190a5

File tree

4 files changed

+847
-0
lines changed

4 files changed

+847
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/
161+
##
162+
## Project files
163+
kms_backup.json
164+
kms_restore.json
165+
kms_print.json
166+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Copyright (c) 2024 Oracle and/or its affiliates.
2+
3+
The Universal Permissive License (UPL), Version 1.0
4+
5+
Subject to the condition set forth below, permission is hereby granted to any
6+
person obtaining a copy of this software, associated documentation and/or data
7+
(collectively the "Software"), free of charge and under any and all copyright
8+
rights in the Software, and any and all patent rights owned or freely
9+
licensable by each licensor hereunder covering either (i) the unmodified
10+
Software as contributed to or provided by such licensor, or (ii) the Larger
11+
Works (as defined below), to deal in both
12+
13+
(a) the Software, and
14+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
one is included with the Software (each a "Larger Work" to which the Software
16+
is contributed by such licensors),
17+
18+
without restriction, including without limitation the rights to copy, create
19+
derivative works of, display, perform, and distribute the Software and make,
20+
use, sell, offer for sale, import, export, have made, and have sold the
21+
Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
either these or other terms.
23+
24+
This license is subject to the following condition:
25+
The above copyright notice and either this complete permission notice or at
26+
a minimum a reference to the UPL must be included in all copies or
27+
substantial portions of the Software.
28+
29+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35+
SOFTWARE.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# OCI Vault software key backup
2+
3+
Owner: Inge Os
4+
5+
OCI Vault supports several types of keys: HSM-based and software-based.
6+
7+
Most services in Oracle OCI with encryption offer two types of encryption key management, Oracle-managed keys or Customer-managed keys.
8+
9+
Some common main requirements most often seen in various cyber security frameworks typically are:
10+
- Separation of Duty, key administration is separated from service management. Typical for Oracle Databases with Transparent Data Encryption the DBA function does not have access to manage the keys, only to read and use them.
11+
- The Master key shall not be stored in any filesystem shared with the encrypted resource. Again, for using the Oracle Database with TDE as an example, the default key management with TDE is to use an Oracle Wallet stored in the same filesystem as the Oracle software installation.
12+
- To meet these requirements and to have a robust operations model for key management, Oracle offers Oracle Key Vault.
13+
14+
Oracle Key Vault offers two main categories of key management:
15+
- HSM-based, with a FIPS compliant Hardware Security Module
16+
- Software-based, where the key is not stored in an HSM, but still protected with the strong tenant isolation and ops isolation that is the cornerstone of Oracle OCI.
17+
18+
The main difference between HSM-based Vaults and Software-based is that the HSM-based prohibits explicit export of the master key. HSM-based key storage offers HA and DR, including cross-region replication, slightly dependent on the type of HSM service deployed.
19+
20+
Software-based Key Vault does not offer cross-region replication.
21+
The main purpose of this asset is to provide an example Python script that demonstrates the usage of the Python KMS SDK, an example of OCI Vault software key backup/restore between two regions.
22+
23+
## Prerequisites
24+
25+
- The script may be run from the Cloud Shell, or from a Linux/Windows environment
26+
- Python 3.0
27+
- Create a virtual environment
28+
29+
```
30+
[user]$ python -m venv ocienv
31+
[user]$ source ocienv/bin/activate
32+
```
33+
- Install Oracle OCI CLI, if not run off Cloud Shell
34+
35+
Please refer to the OCI install guide.
36+
37+
[CLI Installation Guide](https://docs.oracle.com/en-us/iaas/Content/API/Concepts/cliconcepts.htm)
38+
39+
40+
- Install Python SDK into your virtual environment
41+
42+
```
43+
[user]$ pip install oci
44+
```
45+
46+
- Configure OCI profiles and verify the OCI SDK
47+
Configure the OCI CLI with an API key (may be the same) for source and target. Please refer to the OCI CLI documentation for creating an OCI environment file.
48+
49+
50+
[OCI File Configuration]( https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliconfigure.htm)
51+
52+
53+
The OCI CLI configuration may be verified with the following command:
54+
55+
```
56+
[user]$ oci os ns get --profile myprofile
57+
{
58+
"data": "<your namespace>"
59+
}
60+
```
61+
62+
- A valid public key for export only
63+
64+
- Create and/or Check IAM Policies to permit key reading, inspection, and creation.
65+
66+
Permission to `manage` the following types of resources in your Oracle Cloud Infrastructure tenancy: `vaults`, `keys`, `secret-family`
67+
Example policies:
68+
```
69+
Allow group SecurityAdmins to manage vaults in tenancy
70+
71+
Allow group SecurityAdmins to manage keys in tenancy
72+
73+
Allow group SecurityAdmins to manage secret-family in tenancy
74+
```
75+
[Policy Reference, Vault](https://docs.oracle.com/en-us/iaas/Content/Identity/Concepts/commonpolicies.htm#sec-admins-manage-vaults-keys)
76+
77+
78+
If you don't have the required permissions and quota, contact your tenancy administrator. See [Policy Reference](https://docs.cloud.oracle.com/en-us/iaas/Content/Identity/Reference/policyreference.htm), [Service Limits](https://docs.cloud.oracle.com/en-us/iaas/Content/General/Concepts/servicelimits.htm), [Compartment Quotas](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/resourcequotas.htm).
79+
80+
## Script execution
81+
82+
The script requires a number of arguments. The arguments vary depending on the script execution. All or some arguments can be submitted as a configuration file in JSON format or at the command line. Any command-line argument overwrites any arguments from the configuration file,
83+
84+
The script has two modes of operation:
85+
1) Export a key, save it to a file, and print the fingerprint, used for verification of a key
86+
2) Copy the key between two locations where tenant OCID, region, and API Key are required.
87+
88+
A key in the vault may have several versions, and you may specify the OCID of a given version for both export and copy of the key. If no key version is specified, the most recent key is exported.
89+
90+
The script checks if the key is Enabled and is of type SOFTWARE before any export is attempted.
91+
92+
## Configuration parameters/arguments
93+
94+
ociconfig path to OCI configuration file
95+
source_ociprofile Source profile in oci config
96+
source_region Source Region
97+
source_compartment Source Compartment
98+
source_vault Source vault OCI
99+
source_keyname Source Key Name, if search for key
100+
source_key_ocid Source key OCID if search for OCID
101+
source_key_version OCID of a specific key version, if not set pick latest version
102+
103+
target_region Target Region
104+
target_compartment Target Compartment
105+
target_vault Target Vault OCID
106+
target_ociprofile Target profile in oci config
107+
target_keyname Target keyname, the copy will be created with this name
108+
109+
exportonly only save to file or print key, don't backup
110+
wrapping_pubkey_file If it supplied filepointer external key for wrapping
111+
outputfile File for the exported key
112+
113+
## Example execution
114+
Example export
115+
```
116+
python kms_key_backup.py --configfile kms_backup.json --target_keyname test1 --source_key_ocid ocid1.key.oc1.eu-frankfurt-1.xxxxxx --exportonly --wrapping_pubkey_file export_pub.pem --source_key_version ocid1.keyversion.oc1.eu-frankfurt-1.qwertyqwerty
117+
```
118+
with configfile:
119+
```
120+
{
121+
"ociconfig":"/home/myhome/.oci/config",
122+
"source_ociprofile":"my-config-fra",
123+
"source_region" : "eu-frankfurt-1",
124+
"source_compartment" : "ocid1.compartment.oc1..aaaaaaaa",
125+
"source_vault" : "ocid1.vault.oc1.eu-frankfurt-1.aaaeeeqqqlllaleaeqlr",
126+
"wrapping_algorithm" : "RSA_OAEP_AES_SHA256"
127+
}
128+
```
129+
Example backup
130+
```
131+
python kms_key_backup.py --configfile kms_backup.json --target_keyname test2 --source_key_ocid ocid1.key.oc1.eu-frankfurt-1.eeeeaaaaaaa --source_key_version ocid1.keyversion.oc1.eu-frankfurt-1.qfqfqfqfqfaeaea
132+
```
133+
with configfile:
134+
```
135+
{
136+
"ociconfig":"/home/myhome/.oci/config",
137+
"source_ociprofile":"my-config-fra",
138+
"source_region" : "eu-frankfurt-1",
139+
"source_compartment" : "ocid1.compartment.oc1..aaaaaaaa",
140+
"source_vault" : "ocid1.vault.oc1.eu-frankfurt-1.aaaeeeqqqlllaleaeqlr",
141+
"source_key_ocid" : "ocid1.key.oc1.eu-frankfurt-1.sxesxesxesxe",
142+
"target_region" : "eu-stockholm-1",
143+
"target_compartment" : "ocid1.compartment.oc1..aaaaaaaa",
144+
"target_vault" : "ocid1.vault.oc1.eu-stockholm-1.qewuroewrre",
145+
"target_ociprofile":"my-config-ams",
146+
"wrapping_algorithm" : "RSA_OAEP_AES_SHA256"
147+
}
148+
```
149+
150+
151+
152+
153+
# License
154+
155+
Copyright (c) 2024 Oracle and/or its affiliates.
156+
157+
Licensed under the Universal Permissive License (UPL), Version 1.0.
158+
159+
See [LICENSE](https://github.com/oracle-devrel/technology-engineering/blob/main/LICENSE) for more details.
160+
161+

0 commit comments

Comments
 (0)