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

Add script for Private Container Registries (PCR) on AWS #127

Merged
merged 8 commits into from
Feb 28, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixed linting errors
  • Loading branch information
DamaniN committed Feb 13, 2024
commit b269d85b7a8fb5a0e5f40ee0a4984998d8376ad9
111 changes: 59 additions & 52 deletions sample/pcr-aws/pcr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#! /usr/bin/env python3
import argparse, base64, boto3, docker, json, logging, os, pprint, subprocess, sys
import argparse
import base64
import boto3
import docker
import json
import logging
import os
import pprint
import subprocess
import sys
from rubrik_polaris.rubrik_polaris import PolarisClient

pp = pprint.PrettyPrinter(indent=2)
Expand Down Expand Up @@ -33,7 +42,7 @@
### Instantiate with username/password
rubrik = PolarisClient(domain=args.domain, username=args.username, password=args.password, root_domain=args.root_domain,
insecure=args.insecure)

except Exception as err:
print(err)
sys.exit(1)
Expand All @@ -45,7 +54,7 @@
# Setup Docker client

dockerClient = docker.from_env()
docker_api_client = docker.APIClient(base_url='unix://var/run/docker.sock')
docker_api_client = docker.APIClient(base_url='unix://var/run/docker.sock')

# Get Exocompute Bundle (containers)

Expand Down Expand Up @@ -80,7 +89,7 @@

try:
rscEcrToken = rscEcrClient.get_authorization_token(registryIds=[rscRepoFqdn.split('.')[0]])
except Exception as err:
except Exception as err:
print("Error: Unable to get RSC ECR token.")
print(err)
sys.exit(1)
Expand All @@ -103,27 +112,25 @@
# CLI example: "docker pull <Rubrik_ECR_AWS_Account_ID>.dkr.ecr.us-east-1.amazonaws.com/<build_image_name>:<tag>"
print("Pulling " + bundleImages['name'] + " with tag " + bundleImages['tag'])
try:
#response = dockerClient.images.pull(rscRepoFqdn + '/' + bundleImages['name'], tag=bundleImages['tag'])
for line in docker_api_client.pull(rscRepoFqdn + '/' + bundleImages['name'], tag=bundleImages['tag'], stream=True, auth_config=rsc_auth_config_payload, decode=True):
print(line)
logging.info(json.dumps(line, indent=2))
for line in docker_api_client.pull(rscRepoFqdn + '/' + bundleImages['name'], tag=bundleImages['tag'], stream=True, auth_config=rsc_auth_config_payload, decode=True):
print(line)
logging.info(json.dumps(line, indent=2))

except Exception as err:
print("Error: Image pull failed for " + bundleImages['name'] + " with tag " + bundleImages['tag'])
print(err)
sys.exit(1)
print("Error: Image pull failed for " + bundleImages['name'] + " with tag " + bundleImages['tag'])
print(err)
sys.exit(1)
elif bundleImages['sha']:
# CLI example: "docker pull <Rubrik_ECR_AWS_Account_ID>.dkr.ecr.us-east-1.amazonaws.com/<build_image_name>@sha256:<sha>"
print("Pulling " + bundleImages['name'] + " with sha " + bundleImages['sha'])
try:
#response = dockerClient.images.pull(rscRepoFqdn + '/' + bundleImages['name'], tag="sha256:" + bundleImages['sha'])
for line in docker_api_client.pull(rscRepoFqdn + '/' + bundleImages['name'], tag="sha256:" + bundleImages['sha'], stream=True, auth_config=rsc_auth_config_payload, decode=True):
print(line)
logging.info(json.dumps(line, indent=2))
for line in docker_api_client.pull(rscRepoFqdn + '/' + bundleImages['name'], tag="sha256:" + bundleImages['sha'], stream=True, auth_config=rsc_auth_config_payload, decode=True):
print(line)
logging.info(json.dumps(line, indent=2))
except Exception as err:
print("Error: Image pull failed for " + bundleImages['name'] + " with sha " + bundleImages['sha'])
print(err)
sys.exit(1)
print("Error: Image pull failed for " + bundleImages['name'] + " with sha " + bundleImages['sha'])
print(err)
sys.exit(1)
else:
print("Error: No tag or sha found for " + bundleImages['name'] + " in " + rscRepoFqdn + " bundle.")
sys.exit(1)
Expand All @@ -136,19 +143,19 @@
if bundleImages['tag']:
print("Scanning " + bundleImages['name'] + " with tag " + bundleImages['tag'])
try:
print("<Insert Image Scanning Tool Here>")
print("<Insert Image Scanning Tool Here>")
except Exception as err:
print("Error: Image scanning failed for " + bundleImages['name'] + " with tag " + bundleImages['tag'])
print(err)
sys.exit(1)
print("Error: Image scanning failed for " + bundleImages['name'] + " with tag " + bundleImages['tag'])
print(err)
sys.exit(1)
elif bundleImages['sha']:
print("Scanning " + bundleImages['name'] + " with sha " + bundleImages['sha'])
try:
print("<Insert Image Scanning Tool Here>")
print("<Insert Image Scanning Tool Here>")
except Exception as err:
print("Error: Image scanning failed for " + bundleImages['name'] + " with sha " + bundleImages['sha'])
print(err)
sys.exit(1)
print("Error: Image scanning failed for " + bundleImages['name'] + " with sha " + bundleImages['sha'])
print(err)
sys.exit(1)
else:
print("Error: No tag or sha found for " + bundleImages['name'] + " in " + rscRepoFqdn + " bundle.")
sys.exit(1)
Expand All @@ -160,7 +167,7 @@
# Get customer PCR token
# CLI Example "aws ecr get-authorization-token --region <customer_ecr_region>"
try:
customerEcrToken = customerEcrClient.get_authorization_token(registryIds=[pcrFqdn.split('.')[0]])
customerEcrToken = customerEcrClient.get_authorization_token(registryIds=[pcrFqdn.split('.')[0]])
except Exception as err:
print("Error: Unable to get customer PCR token.")
print(err)
Expand All @@ -171,7 +178,7 @@
username, password = base64.b64decode(customerEcrToken['authorizationData'][0]['authorizationToken']).decode('utf-8').split(":")
customer_auth_config_payload = { 'username': username, 'password': password }
customerEcr = dockerClient.login(username=username, password=password, registry=customerEcrToken['authorizationData'][0]['proxyEndpoint'].replace("https://", ""), reauth=True)
except Exception as err:
except Exception as err:
print("Error: Unable to login to customer PCR")
print(err)
sys.exit(1)
Expand Down Expand Up @@ -203,50 +210,50 @@
print("Tagging and pushing " + bundleImages['name'] + " with tag " + bundleImages['tag'])
# CLI Example "docker image tag <Rubrik_ECR_AWS_Account_ID>.dkr.ecr.us-east-1.amazonaws.com/<build_image_name>:<tag><customer_pcr_url>/<build_image_name>:<tag>"
try:
docker_api_client.tag(rscRepoFqdn + '/' + bundleImages['name'] + ":" + bundleImages['tag'], pcrFqdn + '/' + bundleImages['name'] + ":" + bundleImages['tag'])
docker_api_client.tag(rscRepoFqdn + '/' + bundleImages['name'] + ":" + bundleImages['tag'], pcrFqdn + '/' + bundleImages['name'] + ":" + bundleImages['tag'])
except Exception as err:
print("Error: Image tag failed for " + bundleImages['name'] + " with tag " + bundleImages['tag'])
print(err)
sys.exit(1)
print("Error: Image tag failed for " + bundleImages['name'] + " with tag " + bundleImages['tag'])
print(err)
sys.exit(1)
print("Pushing " + bundleImages['name'] + " with tag " + bundleImages['tag'])
# CLI Example "docker push <customer_pcr_url>/<build_image_name>:<tag>"
try:
for line in docker_api_client.push(pcrFqdn + '/' + bundleImages['name'], tag=bundleImages['tag'], stream=True, auth_config=customer_auth_config_payload, decode=True):
print(line)
logging.info(json.dumps(line, indent=2))
for line in docker_api_client.push(pcrFqdn + '/' + bundleImages['name'], tag=bundleImages['tag'], stream=True, auth_config=customer_auth_config_payload, decode=True):
print(line)
logging.info(json.dumps(line, indent=2))
except Exception as err:
print("Error: Image push failed for " + bundleImages['name'] + " with tag " + bundleImages['tag'])
print(err)
sys.exit(1)
print("Error: Image push failed for " + bundleImages['name'] + " with tag " + bundleImages['tag'])
print(err)
sys.exit(1)
elif bundleImages['sha']:
print("Tagging and pushing " + bundleImages['name'] + " with sha " + bundleImages['sha'])
# CLI Example "docker image tag <Rubrik_ECR_AWS_Account_ID>.dkr.ecr.us-east-1.amazonaws.com/<build_image_name>@sha256:<sha> <customer_pcr_url>/<build_image_name>"
# CLI Example "docker image tag <Rubrik_ECR_AWS_Account_ID>.dkr.ecr.us-east-1.amazonaws.com/<build_image_name>@sha256:<sha> <customer_pcr_url>/<build_image_name>"
try:
docker_api_client.tag(rscRepoFqdn + '/' + bundleImages['name'] + "@sha256:" + bundleImages['sha'], pcrFqdn + '/' + bundleImages['name'] )
docker_api_client.tag(rscRepoFqdn + '/' + bundleImages['name'] + "@sha256:" + bundleImages['sha'], pcrFqdn + '/' + bundleImages['name'] )
except Exception as err:
print("Error: Image tag failed for " + bundleImages['name'] + " with sha " + bundleImages['sha'])
print(err)
sys.exit(1)
print("Error: Image tag failed for " + bundleImages['name'] + " with sha " + bundleImages['sha'])
print(err)
sys.exit(1)
print("Pushing " + bundleImages['name'] + " with sha " + bundleImages['sha'])
# CLI Example "docker push <customer_pcr_url>/<build_image_name>@sha256:<sha>"
try:
for line in docker_api_client.push(pcrFqdn + '/' + bundleImages['name'], stream=True, auth_config=customer_auth_config_payload, decode=True):
print(line)
logging.info(json.dumps(line, indent=2))
for line in docker_api_client.push(pcrFqdn + '/' + bundleImages['name'], stream=True, auth_config=customer_auth_config_payload, decode=True):
print(line)
logging.info(json.dumps(line, indent=2))
except Exception as err:
print("Error: Image push failed for " + bundleImages['name'] + " with sha " + bundleImages['sha'])
print(err)
sys.exit(1)
print("Error: Image push failed for " + bundleImages['name'] + " with sha " + bundleImages['sha'])
print(err)
sys.exit(1)

#Accept Container Bundle
variables = {
"input": {
"approvalStatus": "ACCEPTED",
"bundleVersion": "{}".format(exoTaskImageBundle['data']['exotaskImageBundle']['bundleVersion'])
}
}
exoTaskImageBundle = rubrik._query_raw(raw_query='mutation SetBundleApprovalStatus($input: SetBundleApprovalStatusInput!) {setBundleApprovalStatus(input: $input)}',
operation_name=None,
exoTaskImageBundle = rubrik._query_raw(raw_query='mutation SetBundleApprovalStatus($input: SetBundleApprovalStatusInput!) {setBundleApprovalStatus(input: $input)}',
operation_name=None,
# variables={'"input": {"approvalStatus": "ACCEPTED","bundleVersion": {}}'.format(exoTaskImageBundle['data']['exotaskImageBundle']['bundleVersion'])},
variables=variables,
timeout=60)
Loading