-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding kms policy name for same kms encryption keys #11497
Open
shivamdurgbuns
wants to merge
7
commits into
red-hat-storage:master
Choose a base branch
from
shivamdurgbuns:issue/11495
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−23
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c91590b
Adding kms policy name for same kms encryption keys
shivamdurgbuns 13b7cba
Adding vault_policy_name to the create policy function
shivamdurgbuns 6ff1866
Updating if loop
shivamdurgbuns e20b5a7
Adding same token policy feature to Vault Class
shivamdurgbuns 44832c7
Removing conf key
shivamdurgbuns 327fa82
Removing function call
shivamdurgbuns ff28082
Appending secondary cluster backend path to the same policy
shivamdurgbuns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,7 +129,7 @@ def __init__(self): | |
"VAULT_BACKEND", defaults.VAULT_DEFAULT_BACKEND_VERSION | ||
) | ||
self.kmsid = None | ||
self.vault_policy_name = None | ||
self.vault_policy_name = self.kms_vault_policy_name | ||
self.vault_kube_auth_path = "kubernetes" | ||
self.vault_kube_auth_role = constants.VAULT_KUBERNETES_AUTH_ROLE | ||
self.vault_kube_auth_namespace = None | ||
|
@@ -147,6 +147,16 @@ def vault_path_token(self, value): | |
# For setting values in test cases | ||
Vault._vault_path_token = value | ||
|
||
@property | ||
def kms_vault_policy_name(self): | ||
cluster_name = config.ENV_DATA.get("cluster_name") | ||
if config.multicluster: | ||
with config.RunWithPrimaryConfigContext(): | ||
cluster_name = config.ENV_DATA.get("cluster_name") | ||
policy_name = f"kpn_{cluster_name}" | ||
logger.info(f"Vault policy name will be {policy_name}") | ||
return policy_name | ||
|
||
def deploy(self): | ||
""" | ||
This function delegates the deployment of vault | ||
|
@@ -555,7 +565,7 @@ def vault_create_backend_path(self, backend_path=None, kv_version=None): | |
f"Failed to create path f{self.vault_backend_path}" | ||
) | ||
if not backend_path: | ||
self.vault_create_policy() | ||
self.vault_create_policy(policy_name=self.vault_policy_name) | ||
|
||
def vault_create_policy(self, policy_name=None): | ||
""" | ||
|
@@ -565,17 +575,40 @@ def vault_create_policy(self, policy_name=None): | |
VaultOperationError exception | ||
""" | ||
policy = ( | ||
f'path "{self.vault_backend_path}/*" {{\n' | ||
f' capabilities = ["create", "read", "update","delete"]' | ||
f"\n}}\n" | ||
f'path "sys/mounts" {{\n' | ||
f'capabilities = ["read"]\n' | ||
f"}}" | ||
) | ||
vault_hcl = tempfile.NamedTemporaryFile(mode="w+", prefix="test", delete=False) | ||
with open(vault_hcl.name, "w") as hcl: | ||
hcl.write(policy) | ||
# Check if policy still exists | ||
cmd_list_policy = "vault policy list --format=json" | ||
|
||
out = subprocess.check_output(shlex.split(cmd_list_policy)) | ||
json_out = json.loads(out) | ||
if self.vault_policy_name in json_out: | ||
# if policy already exists append the secondary cluster backend path to the policy | ||
poilcy_data = ( | ||
f"\n}}\n" | ||
f'path "{self.vault_backend_path}/*" {{\n' | ||
f' capabilities = ["create", "read", "update","delete"]' | ||
) | ||
vault_hcl = tempfile.NamedTemporaryFile( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and the rest from this line can be deleted if you will follow the suggestion from code above |
||
mode="a+", prefix="test", delete=False | ||
) | ||
logger.info( | ||
f"Appending secondary cluster backend path to policy: {self.vault_policy_name}" | ||
) | ||
with open(vault_hcl.name, "a") as hcl: | ||
hcl.write(poilcy_data) | ||
else: | ||
policy = ( | ||
f'path "{self.vault_backend_path}/*" {{\n' | ||
f' capabilities = ["create", "read", "update","delete"]' | ||
f"\n}}\n" | ||
f'path "sys/mounts" {{\n' | ||
f'capabilities = ["read"]\n' | ||
f"}}" | ||
) | ||
vault_hcl = tempfile.NamedTemporaryFile( | ||
mode="w+", prefix="test", delete=False | ||
) | ||
with open(vault_hcl.name, "w") as hcl: | ||
hcl.write(policy) | ||
|
||
if policy_name: | ||
self.vault_policy_name = policy_name | ||
|
@@ -808,20 +841,25 @@ def remove_vault_policy(self, vault_namespace=None): | |
Args: | ||
vault namespace (str): Namespace in Vault, if exists, where the backend path is created | ||
""" | ||
|
||
if vault_namespace: | ||
cmd = f"vault policy delete -namespace={vault_namespace} {self.vault_policy_name} " | ||
else: | ||
cmd = f"vault policy delete {self.vault_policy_name}" | ||
subprocess.check_output(shlex.split(cmd)) | ||
|
||
# Check if policy still exists | ||
if vault_namespace: | ||
cmd = f"vault policy list -namespace={vault_namespace} --format=json" | ||
cmd_list_policy = ( | ||
f"vault policy list -namespace={vault_namespace} --format=json" | ||
) | ||
else: | ||
cmd = "vault policy list --format=json" | ||
cmd_list_policy = "vault policy list --format=json" | ||
|
||
out = subprocess.check_output(shlex.split(cmd)) | ||
out = subprocess.check_output(shlex.split(cmd_list_policy)) | ||
json_out = json.loads(out) | ||
if self.vault_policy_name in json_out: | ||
if vault_namespace: | ||
cmd = f"vault policy delete -namespace={vault_namespace} {self.vault_policy_name} " | ||
else: | ||
cmd = f"vault policy delete {self.vault_policy_name}" | ||
subprocess.check_output(shlex.split(cmd)) | ||
|
||
# Check if policy still exists | ||
out = subprocess.check_output(shlex.split(cmd_list_policy)) | ||
json_out = json.loads(out) | ||
if self.vault_policy_name in json_out: | ||
raise KMSResourceCleaneupError( | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo in poilcy_data
and you are repeating this unnecesarly. Should be defined once only.
This is simplified how you can do this: