-
-
Notifications
You must be signed in to change notification settings - Fork 115
Create credentials upload plugins for GCP and AWS (#438) #439
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
Draft
dbalabka
wants to merge
1
commit into
dask:main
Choose a base branch
from
dbalabka:credentials-plugins
base: main
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.
Draft
Changes from all commits
Commits
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 hidden or 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
|
||
from distributed import WorkerPlugin | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class UploadAWSCredentials(WorkerPlugin): | ||
# """Automatically upload a GCP key to the worker.""" | ||
|
||
name = "upload_aws_credentials" | ||
|
||
def __init__(self): | ||
""" | ||
Initialize the plugin by reading in the data from the given file. | ||
""" | ||
config_path = os.getenv("AWS_CONFIG_FILE", Path.home() / Path(".aws/config")) | ||
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. Should why upload the complete config? |
||
credentials_path = os.getenv( | ||
"AWS_SHARED_CREDENTIALS_FILE", Path.home() / Path(".aws/credentials") | ||
) | ||
config_path, credentials_path = Path(config_path), Path(credentials_path) | ||
|
||
if not config_path.exists(): | ||
raise ValueError( | ||
f"Config file {config_path} does not exist. If you store AWS config " | ||
"in a different location, please set AWS_CONFIG_FILE environment variable." | ||
) | ||
|
||
if not credentials_path.exists(): | ||
raise ValueError( | ||
f"Credentials file {credentials_path} does not exist. If you store AWS credentials " | ||
"in a different location, please set AWS_SHARED_CREDENTIALS_FILE environment variable." | ||
) | ||
|
||
self.config_filename = config_path.name | ||
self.credentials_filename = credentials_path.name | ||
|
||
with open(config_path, "rb") as f: | ||
self.config = f.read() | ||
with open(credentials_path, "rb") as f: | ||
self.credentials = f.read() | ||
|
||
async def setup(self, worker): | ||
await worker.upload_file( | ||
filename=self.config_filename, data=self.config, load=False | ||
) | ||
worker_config_path = Path(worker.local_directory) / self.config_filename | ||
os.environ["AWS_CONFIG_FILE"] = str(worker_config_path) | ||
|
||
await worker.upload_file( | ||
filename=self.credentials_filename, data=self.credentials, load=False | ||
) | ||
worker_credentials_path = ( | ||
Path(worker.local_directory) / self.credentials_filename | ||
) | ||
os.environ["AWS_SHARED_CREDENTIALS_FILE"] = str(worker_credentials_path) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
|
||
from distributed import WorkerPlugin | ||
from google.auth._cloud_sdk import get_application_default_credentials_path | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class UploadGCPKey(WorkerPlugin): | ||
"""Automatically upload a GCP key to the worker.""" | ||
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. Need to add more context about why we need to upload the key from the laptop. |
||
|
||
name = "upload_gcp_key" | ||
|
||
def __init__(self): | ||
""" | ||
Initialize the plugin by reading in the data from the given file. | ||
""" | ||
key_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") | ||
if key_path is None: | ||
key_path = Path(get_application_default_credentials_path()) | ||
if not key_path.exists(): | ||
raise ValueError("GOOGLE_APPLICATION_CREDENTIALS is not set or `gcloud auth application-default login` wasn't run.") | ||
|
||
key_path = Path(key_path) | ||
self.filename = key_path.name | ||
|
||
logger.info("Uploading GCP key from %s.", str(key_path)) | ||
|
||
with open(key_path, "rb") as f: | ||
self.data = f.read() | ||
|
||
async def setup(self, worker): | ||
await worker.upload_file(filename=self.filename, data=self.data, load=False) | ||
worker_key_path = Path(worker.local_directory) / self.filename | ||
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = str(worker_key_path) |
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.
I have to add some logging for the visibility of the fact that file has been uploaded