Ask Your Question
0

Is it impossible to retrieve secrets saved in Secret Manager using cloud dataproc (Pyspark)?

asked 2022-03-01 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-30 12:00:00 +0000

lakamha gravatar image

No, it is not impossible to retrieve secrets saved in Secret Manager using cloud dataproc (Pyspark). You can use the Secret Manager API to retrieve the secrets and pass them to your Pyspark code. Here's an example:

First, authenticate with the Secret Manager API using the appropriate credentials:

from google.cloud import secretmanager

# Authenticate with the Secret Manager API
client = secretmanager.SecretManagerServiceClient()

Next, retrieve the secret:

# Retrieve the secret
project_id = '<your-project-id>'
secret_id = '<your-secret-id>'
version_id = '<your-version-id>' # optional

name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
response = client.access_secret_version(request={"name": name})

# Extract the secret value
secret_string = response.payload.data.decode('UTF-8')

Finally, pass the secret to your Pyspark code:

from pyspark.sql import SparkSession

spark = SparkSession.builder \
    .appName("MyApp") \
    .config("spark.some.config.option", "some-value") \
    .config("mysecret", secret_string) \ # pass the secret as a Spark configuration option
    .getOrCreate()
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-01 11:00:00 +0000

Seen: 8 times

Last updated: Sep 30 '22