GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — server/agent/resolve_url.py at main · PastureStack/server

Expand file tree

/

resolve_url.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
executable file
·
145 lines (117 loc) · 4.75 KB
/

resolve_url.py

Copy path

File metadata and controls

executable file
·
145 lines (117 loc) · 4.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
import json
import sys
import time
from urllib import parse, request
from control_plane_url import ControlPlaneURLPolicy
def fail(message):
print(message, file=sys.stderr)
raise SystemExit(1)
class ControlPlaneClient:
def __init__(self, base_url):
self.policy = ControlPlaneURLPolicy(base_url)
def http(self, method, url, body=None):
checked_url = self.policy.validate(url)
data = None
headers = {"Accept": "application/json"}
if body is not None:
data = json.dumps(body).encode("utf-8")
headers["Content-Type"] = "application/json"
req = request.Request(
checked_url, data=data, headers=headers, method=method
)
with self.policy.open(req, timeout=60) as resp:
return resp.status, dict(resp.headers), resp.read().decode("utf-8")
def http_json(self, method, url, body=None):
status, headers, text = self.http(method, url, body)
if status < 200 or status >= 300:
fail("PastureStack API request failed with status {}".format(status))
return json.loads(text), headers
def base_from_schemas(url):
return url[:-8] if url.endswith("/schemas") else url.rstrip("/")
def collection_url(client, api_base, schema_name):
schema, _ = client.http_json(
"GET", "{}/schemas/{}".format(api_base.rstrip("/"), schema_name)
)
return schema.get("links", {}).get("collection")
def has_create(schema):
methods = schema.get("collectionMethods") or []
return "POST" in methods
def wait_success(client, resource, timeout=300):
deadline = time.time() + timeout
current = resource
while time.time() < deadline:
state = current.get("state")
transitioning = current.get("transitioning")
if state == "active" and transitioning in (None, "no"):
return current
if transitioning == "error":
fail(
"Registration token failed: {}".format(
current.get("transitioningMessage", "unknown error")
)
)
self_url = current.get("links", {}).get("self")
if not self_url:
return current
time.sleep(2)
current, _ = client.http_json("GET", self_url)
fail("Timed out waiting for registration token")
def active_registration_token(client, api_base):
collection = collection_url(client, api_base, "registrationToken")
if not collection:
fail("Failed to find registrationToken collection")
query = parse.urlencode({"state": "active"})
tokens, _ = client.http_json("GET", "{}?{}".format(collection, query))
data = tokens.get("data", [])
if data:
return data[0]
token, _ = client.http_json("POST", collection, {})
return wait_success(client, token)
def resolve_registration_url(url):
client = ControlPlaneClient(url)
initial_url = client.policy.validate(url)
status, headers, text = client.http("GET", initial_url)
if status == 200 and text.startswith("#!/bin/sh"):
return initial_url
schemas_url = (
headers.get("X-API-Schemas") or headers.get("x-api-schemas") or initial_url
)
schemas_url = client.policy.validate(schemas_url)
api_base = base_from_schemas(schemas_url)
registration_schema, _ = client.http_json(
"GET", "{}/schemas/registrationToken".format(api_base.rstrip("/"))
)
if not has_create(registration_schema):
projects_url = collection_url(client, api_base, "project")
if not projects_url:
fail("Failed to find project collection")
projects, _ = client.http_json(
"GET",
"{}?{}".format(
projects_url, parse.urlencode({"uuid": "adminProject"})
),
)
data = projects.get("data", [])
if not data:
fail("Failed to find admin resource group")
schemas_url = data[0].get("links", {}).get("schemas")
if not schemas_url:
fail("Failed to find admin resource group schemas")
api_base = base_from_schemas(client.policy.validate(schemas_url))
token = active_registration_token(client, api_base)
registration_url = token.get("registrationUrl") or token.get("links", {}).get(
"registrationUrl"
)
if not registration_url:
fail("Registration token response did not include registrationUrl")
return client.policy.validate(registration_url)
def main(argv):
if len(argv) != 2:
fail("Usage: resolve_url.py CONTROL_PLANE_URL")
try:
print(resolve_registration_url(argv[1]))
except (ValueError, OSError, json.JSONDecodeError) as exc:
fail("Failed to resolve the PastureStack registration URL: {}".format(exc))
if __name__ == "__main__":
main(sys.argv)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.