-
Notifications
You must be signed in to change notification settings - Fork 695
Add argument --iam-token-file in ydb-dstool #20303
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
Open
kruall
wants to merge
6
commits into
ydb-platform:main
Choose a base branch
from
kruall:dstool/add_iam_token_file
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.
+52
−18
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
02133f9
Add iam token argument
kruall 89e5efb
improve token parsing
kruall 680decc
fix
kruall 19aef9f
rewrite parse token method
kruall d40e6d6
fix bug in parse_token_value
kruall db493fe
fix missed try except block
kruall 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 | ||||
---|---|---|---|---|---|---|
|
@@ -152,26 +152,58 @@ def make_url(self, endpoint, path, params): | |||||
location = endpoint.host_with_port | ||||||
return urllib.parse.urlunsplit((endpoint.protocol, location, path, urllib.parse.urlencode(params), '')) | ||||||
|
||||||
def parse_token(self, token_file): | ||||||
def parse_token(self, token_file, iam_token_file=None): | ||||||
if token_file: | ||||||
self.token = token_file.readline().rstrip('\r\n') | ||||||
self.token_type, self.token = self.read_token_from_file(token_file, 'OAuth') | ||||||
token_file.close() | ||||||
if self.token is None: | ||||||
self.token = os.getenv('YDB_TOKEN') | ||||||
return | ||||||
|
||||||
if iam_token_file: | ||||||
self.token_type, self.token = self.read_token_from_file(iam_token_file, 'Bearer') | ||||||
iam_token_file.close() | ||||||
return | ||||||
|
||||||
token_value = os.getenv('YDB_TOKEN') | ||||||
if token_value is not None: | ||||||
self.token_type, self.token = self.parse_token_value(token_value, 'OAuth') | ||||||
return | ||||||
|
||||||
token_value = os.getenv('IAM_TOKEN') | ||||||
if token_value is not None: | ||||||
self.token_type, self.token = self.parse_token_value(token_value, 'Bearer') | ||||||
return | ||||||
|
||||||
default_token_paths = [ | ||||||
('OAuth', os.path.expanduser(os.path.join('~', '.ydb', 'token'))), | ||||||
('Bearer', os.path.expanduser(os.path.join('~', '.ydb', 'iam_token'))), | ||||||
] | ||||||
for token_type, token_file_path in default_token_paths: | ||||||
self.token_type, self.token = self.read_token_file(token_file_path, token_type) | ||||||
if self.token is not None: | ||||||
self.token = self.token.strip() | ||||||
if self.token is None: | ||||||
try: | ||||||
path = os.path.expanduser(os.path.join('~', '.ydb', 'token')) | ||||||
with open(path) as f: | ||||||
self.token = f.readline().strip('\r\n') | ||||||
except Exception: | ||||||
pass | ||||||
|
||||||
if self.token is not None and len(self.token.split(' ')) == 2: | ||||||
self.token_type, self.token = self.token.split(' ') | ||||||
return | ||||||
|
||||||
def read_token_from_file(self, token_file, default_token_type): | ||||||
if token_file is None: | ||||||
return default_token_type, None | ||||||
token_value = token_file.readline().rstrip('\r\n') | ||||||
return self.parse_token_value(token_value, default_token_type) | ||||||
|
||||||
def read_token_file(self, token_file_path, default_token_type): | ||||||
if token_file_path is None: | ||||||
return default_token_type, None | ||||||
try: | ||||||
return self.read_token_from_file_and_close(open(token_file_path, 'r'), default_token_type) | ||||||
except Exception: | ||||||
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. Avoid a bare
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
return default_token_type, None | ||||||
|
||||||
def parse_token_value(self, token_value, default_token_type): | ||||||
if token_value is None: | ||||||
return default_token_type, None | ||||||
splitted = token_value.strip().split(' ') | ||||||
if len(splitted) == 2: | ||||||
return splitted | ||||||
else: | ||||||
self.token_type = 'OAuth' | ||||||
return default_token_type, token_value | ||||||
|
||||||
def apply_args(self, args, with_localhost=True): | ||||||
self.args = args | ||||||
|
@@ -199,7 +231,7 @@ def apply_args(self, args, with_localhost=True): | |||||
if 'http' not in protocols and 'https' in protocols: | ||||||
self.mon_protocol = 'https' | ||||||
|
||||||
self.parse_token(args.token_file) | ||||||
self.parse_token(args.token_file, args.iam_token_file) | ||||||
self.domain = 1 | ||||||
self.verbose = args.verbose or args.debug | ||||||
self.debug = args.debug | ||||||
|
@@ -218,7 +250,9 @@ def add_host_access_options(self, parser, with_endpoint=True): | |||||
g.add_argument('--endpoint', '-e', metavar='[PROTOCOL://]HOST[:PORT]', type=str, required=True, action='append', help=ConnectionParams.ENDPOINT_HELP) | ||||||
g.add_argument('--grpc-port', type=int, default=2135, metavar='PORT', help='GRPC port to use for procedure invocation') | ||||||
g.add_argument('--mon-port', type=int, default=8765, metavar='PORT', help='HTTP monitoring port for viewer JSON access') | ||||||
g.add_argument('--token-file', type=FileType(encoding='ascii'), metavar='PATH', help='Path to token file') | ||||||
token_group = g.add_mutually_exclusive_group() | ||||||
token_group.add_argument('--token-file', type=FileType(encoding='ascii'), metavar='PATH', help='Path to token file') | ||||||
token_group.add_argument('--iam-token-file', type=FileType(encoding='ascii'), metavar='PATH', help='Path to IAM token file') | ||||||
g.add_argument('--ca-file', metavar='PATH', dest='cafile', type=str, help='File containing PEM encoded root certificates for SSL/TLS connections. ' | ||||||
'If this parameter is empty, the default roots will be used.') | ||||||
g.add_argument('--http-timeout', type=int, default=5, help='Timeout for blocking socket I/O operations during HTTP(s) queries') | ||||||
|
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.
Uh oh!
There was an error while loading. Please reload this page.