Skip to content
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

refactor _get_k8s_auth -> _get_k8s_auth_* #54

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 155 additions & 135 deletions secretupdater/secretupdater/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,156 +260,176 @@ def _get_k8s_auth(credentials):

for kube in k8s_clusters:
id = kube.get('id')

if _get_credential(kube, 'certificate-authority-data'): # Assuming that if we are given the k8s CA, we're using certs
app.logger.debug("%s: Found CA data, assuming we're using x509 for auth" % (id))
config = {
"clusters": [
{
"name": id,
"cluster": {
"server": kube.get('metadata').get('endpoint'),
"certificate-authority-data": _get_credential(kube, 'certificate-authority-data'),
},
},
],
"contexts": [
{
"name": id,
"context": {
"cluster": id,
"user": "admin"
},
}
],
"current-context": id,
"users": [
{
"name": "admin",
"user": {
"username": _get_credential(kube, 'username'),
"client-certificate-data": _get_credential(kube, 'client-certificate-data'),
"client-key-data": _get_credential(kube, 'client-key-data'),
},
}
],
}
config = _get_k8s_auth_ca(kube)

elif _get_credential(kube, 'eks-certificate-authority-data'):
app.logger.debug("%s: Found EKS CA data" % (id))
try:
eks_cluster_id = re.sub(r'[^a-zA-Z0-9\s\-]', '', kube.get('metadata').get('eks-cluster-id'))
eks_endpoint = kube.get('metadata').get('endpoint')
eks_assume_role_arn = re.sub(r'[^a-zA-Z0-9\s\-\:\/]', '', kube.get('metadata').get('eks-assume-role-arn'))
eks_output = sp.getoutput(f"aws eks get-token --cluster-name {eks_cluster_id} --role {eks_assume_role_arn}")
app.logger.debug(eks_output)
except TypeError:
raise ClusterAttributeError("Cluster credential or metadata misconfigured or not found")
config = _get_k8s_auth_eks(kube)

try:
json_token = json.loads(eks_output)
token_data = dict(filter(lambda x: "status" in x, json_token.items()))
token = token_data['status']['token']
app.logger.debug(token)
except ValueError:
raise BadEKSToken("Unable to obtain a valid token aws-cli")

config = {
"clusters": [
{
"name": eks_cluster_id,
"cluster": {
"server": eks_endpoint,
"certificate-authority-data": _get_credential(kube, 'eks-certificate-authority-data'),
},
},
],
"contexts": [
{
"name": eks_cluster_id,
"context": {
"cluster": eks_cluster_id,
"user": "admin"
},
}
],
"current-context": eks_cluster_id,
"users": [
{
"name": "admin",
"user": {
"token": token
},
},
],
}
elif _get_credential(kube, 'token'):
app.logger.debug("%s: Kubernetes token provided" % (id))
config = {
"clusters": [
{
"name": id,
"cluster": {
"server": kube.get('metadata').get('endpoint'),
"insecure-skip-tls-verify": True
},
},
],
"contexts": [
{
"name": id,
"context": {
"cluster": id,
"user": "admin"
},
}
],
"current-context": id,
"users": [
{
"name": "admin",
"user": {
"token": _get_credential(kube, 'token')
}
}
]
}
config = _get_k8s_auth_token(kube)

elif _get_credential(kube, 'username'):
app.logger.debug("%s: Kubernetes username/password provided" % (id))
config = {
"clusters": [
{
"name": id,
"cluster": {
"server": kube.get('metadata').get('endpoint'),
"insecure-skip-tls-verify": True,
},
},
],
"contexts": [
{
"name": id,
"context": {
"cluster": id,
"user": "admin"
},
}
],
"current-context": id,
"users": [
{
"name": "admin",
"user": {
"username": _get_credential(kube, 'username'),
"password": _get_credential(kube, 'password'),
},
}
],
}
config = _get_k8s_auth_userpass(kube)

k8s_auth.append(config)

return k8s_auth


def _get_k8s_auth_ca(kube):
return {
"clusters": [
{
"name": id,
"cluster": {
"server": kube.get('metadata').get('endpoint'),
"certificate-authority-data": _get_credential(kube, 'certificate-authority-data'),
},
},
],
"contexts": [
{
"name": id,
"context": {
"cluster": id,
"user": "admin"
},
}
],
"current-context": id,
"users": [
{
"name": "admin",
"user": {
"username": _get_credential(kube, 'username'),
"client-certificate-data": _get_credential(kube, 'client-certificate-data'),
"client-key-data": _get_credential(kube, 'client-key-data'),
},
}
],
}


def _get_k8s_auth_eks(kube):
try:
eks_cluster_id = re.sub(r'[^a-zA-Z0-9\s\-]', '', kube.get('metadata').get('eks-cluster-id'))
eks_endpoint = kube.get('metadata').get('endpoint')
eks_assume_role_arn = re.sub(r'[^a-zA-Z0-9\s\-\:\/]', '', kube.get('metadata').get('eks-assume-role-arn'))
eks_output = sp.getoutput(f"aws eks get-token --cluster-name {eks_cluster_id} --role {eks_assume_role_arn}")
app.logger.debug(eks_output)
except TypeError:
raise ClusterAttributeError("Cluster credential or metadata misconfigured or not found")

try:
json_token = json.loads(eks_output)
token_data = dict(filter(lambda x: "status" in x, json_token.items()))
token = token_data['status']['token']
app.logger.debug(token)
except ValueError:
raise BadEKSToken("Unable to obtain a valid token aws-cli")

return {
"clusters": [
{
"name": eks_cluster_id,
"cluster": {
"server": eks_endpoint,
"certificate-authority-data": _get_credential(kube, 'eks-certificate-authority-data'),
},
},
],
"contexts": [
{
"name": eks_cluster_id,
"context": {
"cluster": eks_cluster_id,
"user": "admin"
},
}
],
"current-context": eks_cluster_id,
"users": [
{
"name": "admin",
"user": {
"token": token
},
},
],
}


def _get_k8s_auth_token(kube):
return {
"clusters": [
{
"name": id,
"cluster": {
"server": kube.get('metadata').get('endpoint'),
"insecure-skip-tls-verify": True
},
},
],
"contexts": [
{
"name": id,
"context": {
"cluster": id,
"user": "admin"
},
}
],
"current-context": id,
"users": [
{
"name": "admin",
"user": {
"token": _get_credential(kube, 'token')
}
}
]
}


def _get_k8s_auth_userpass(kube):
return {
"clusters": [
{
"name": id,
"cluster": {
"server": kube.get('metadata').get('endpoint'),
"insecure-skip-tls-verify": True,
},
},
],
"contexts": [
{
"name": id,
"context": {
"cluster": id,
"user": "admin"
},
}
],
"current-context": id,
"users": [
{
"name": "admin",
"user": {
"username": _get_credential(kube, 'username'),
"password": _get_credential(kube, 'password'),
},
}
],
}

mikelorant marked this conversation as resolved.
Show resolved Hide resolved

class NamespaceNoDeploymentError(Exception):
"""A Namespace does not have any Deployment objects"""

Expand Down