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

A little amuse bouche for creating a token #135

Merged
merged 4 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions all-the-things.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

./ghe2json.py
./get-token.py
./configure.py --primer build-all.sh
60 changes: 60 additions & 0 deletions get-token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3

from playwright.sync_api import sync_playwright
import json
import time

def run(playwright):
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()

# Load environment.json for URL and admin_user
with open('environment.json') as f:
data = json.load(f)
base_url = data['hostname']
admin_user = data['admin_user']
token_field = 'token'

# Navigate to the URL
page.goto(f"https://{base_url}")

# Sign in
page.wait_for_selector('input[name="login"]') # Ensure login input is ready
page.fill('input[name="login"]', admin_user)
page.fill('input[name="password"]', data['admin_password'])

# Click the sign-in button using a more specific selector
page.click('input[type="submit"][name="commit"][value="Sign in"]', timeout=5000)

page.goto(f"https://{base_url}/settings/tokens/new")

# Wait for the token description input to be ready and fill it
page.wait_for_selector('input[name="oauth_access[description]"]') # Ensure the input is ready
# make "the-power" a template variable with a unix timestamp onthe end

description_value = 'the-power-' + str(int(time.time()))
page.fill('input[name="oauth_access[description]"]', description_value)

checkboxes = page.query_selector_all('input[type="checkbox"]')
for checkbox in checkboxes:
checkbox.check()

page.click('button.btn-primary:has-text("Generate token")', timeout=5000)

# Wait for the token to be visible and retrieve its value
token_value = page.inner_text('#new-oauth-token')
print(f"Retrieved token: {token_value}")

# Load the current environment.json into memory
with open('environment.json', 'r') as f:
data = json.load(f)

# Update the token field with the new token value
data['token'] = token_value

# Write the updated dictionary back to environment.json
with open('environment.json', 'w') as f:
json.dump(data, f, indent=4)

with sync_playwright() as playwright:
run(playwright)
11 changes: 9 additions & 2 deletions thepower.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def ghe2json(text, ssh=True):
lexer = shlex.shlex(text)
lexer.whitespace_split = True
lexer.whitespace = ' \t\n\r\f\v'
tokens = list(lexer)
tokens = list(lexer)

# Find the index of "terminated" in the token list
terminated_index = tokens.index("terminated")

Expand Down Expand Up @@ -88,6 +88,12 @@ def ghe2json(text, ssh=True):
parsed_url = urlparse(http_token)
hostname = (parsed_url.hostname)

# Admin user
admin_index = tokens.index("(Log")
admin_user = str(tokens[admin_index+3]).strip("'")



# Personal Access Token (PAT)
pat = 'unknown'
pat = next((token for token in tokens if token.startswith("ghp_")), '')
Expand Down Expand Up @@ -132,6 +138,7 @@ def ghe2json(text, ssh=True):
environment['ip_addresses'] = ip_addresses
environment['ip_primary'] = primary
environment['ip_replica'] = secondary
environment['admin_user'] = admin_user
environment['token_generate_url'] = f"https://{hostname}/settings/tokens/new"

return json.dumps(environment)
Expand Down
Loading