Skip to content

Commit 1e5b0b7

Browse files
authored
drop access key support from the sdk (#114)
1 parent bbf8086 commit 1e5b0b7

File tree

4 files changed

+1
-102
lines changed

4 files changed

+1
-102
lines changed

railib/config.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
# and client credentials.
2020

2121
import configparser
22-
import json
2322
import os
2423
from pathlib import Path
2524

26-
from .credentials import AccessKeyCredentials, ClientCredentials
25+
from .credentials import ClientCredentials
2726

2827

2928
def _read_config_profile(fname: str, profile: str) -> dict:
@@ -35,27 +34,6 @@ def _read_config_profile(fname: str, profile: str) -> dict:
3534
return {k: config[profile][k] for k in config[profile]}
3635

3736

38-
def _read_pkey(fname: Path):
39-
with open(fname) as fp:
40-
data = json.load(fp)
41-
pkey = data.get("sodium", {}).get("seed", None)
42-
if pkey is None:
43-
raise Exception("malformed private key")
44-
return pkey
45-
46-
47-
# Reads access key credentials from the config file. Returns None if they
48-
# do not exist.
49-
def _read_access_key_credentials(data, path: Path):
50-
akey = data.get("access_key", None)
51-
if akey is not None:
52-
fname = data.get("private_key_filename", None)
53-
if fname is not None:
54-
pkey = _read_pkey(path.with_name(fname))
55-
return AccessKeyCredentials(akey, pkey)
56-
return None
57-
58-
5937
# Read client credentials from the config file. Returns None if they do not
6038
# exist.
6139
def _read_client_credentials(data):
@@ -72,8 +50,6 @@ def _read_client_credentials(data):
7250
# if they exist. Returns None if no credentials exist.
7351
def _read_credentials(data, path):
7452
creds = _read_client_credentials(data)
75-
if creds is None:
76-
creds = _read_access_key_credentials(data, path)
7753
return creds
7854

7955

railib/credentials.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
__all__ = [
2020
"Credentials",
21-
"AccessKeyCredentials",
2221
"AccessToken",
2322
"ClientCredentials",
2423
]
@@ -31,13 +30,6 @@ class Credentials(ABC):
3130
pass
3231

3332

34-
# Represents access key credentials.
35-
class AccessKeyCredentials(Credentials):
36-
def __init__(self, akey: str, pkey: str):
37-
self.akey = akey # access_key
38-
self.pkey = pkey # private_key
39-
40-
4133
# Represents an OAuth access token.
4234
class AccessToken:
4335
def __init__(self, access_token: str, scope: str, expires_in: int, created_on: float = time.time()):

railib/rest.py

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@
1414

1515
"""Low level HTTP interface to the RelationalAI REST API."""
1616

17-
import ed25519
18-
import base64
19-
from datetime import datetime
20-
import hashlib
2117
import json
2218
from os import path
2319
from urllib.parse import urlencode, urlsplit, quote
2420
from urllib.request import Request, urlopen
2521

2622
from .__init__ import __version__
2723
from .credentials import (
28-
AccessKeyCredentials,
2924
AccessToken,
3025
Credentials,
3126
ClientCredentials,
@@ -44,9 +39,6 @@
4439
SCOPE = "scope"
4540

4641

47-
_empty = bytes("", encoding="utf8")
48-
49-
5042
# Context contains the state required to make rAI REST API calls.
5143
class Context(object):
5244
def __init__(self, region: str = None, credentials: Credentials = None):
@@ -202,63 +194,6 @@ def _request_access_token(ctx: Context, url: str) -> AccessToken:
202194
raise Exception("failed to get the access token")
203195

204196

205-
# Implement RAI API key authentication by signing the request and adding the
206-
# required authorization header.
207-
def _sign(ctx: Context, req: Request) -> None:
208-
assert isinstance(ctx.credentials, AccessKeyCredentials)
209-
210-
ts = datetime.utcnow()
211-
212-
# ISO8601 date/time strings for time of request
213-
signature_date = ts.strftime("%Y%m%dT%H%M%SZ")
214-
scope_date = ts.strftime("%Y%m%d")
215-
216-
# Authentication scope
217-
scope = f"{scope_date}/{ctx.region}/{ctx.service}/rai01_request"
218-
219-
# SHA256 hash of content
220-
content = req.data or _empty
221-
content_hash = hashlib.sha256(content).hexdigest()
222-
223-
# Include "x-rai-date" in signed headers
224-
req.headers["x-rai-date"] = signature_date
225-
226-
# Sort and lowercase headers to produce a canonical form
227-
canonical_headers = sorted(
228-
[f"{k.lower()}:{v.strip()}" for k, v in req.headers.items()]
229-
)
230-
231-
h_names = sorted([k.lower() for k in req.headers])
232-
signed_headers = ";".join(h_names)
233-
234-
# Create hash of canonical request
235-
split_result = urlsplit(req.full_url) # was self.url
236-
canonical_form = "{}\n{}\n{}\n{}\n\n{}\n{}".format(
237-
req.method,
238-
_encode_path(split_result.path),
239-
split_result.query,
240-
"\n".join(canonical_headers),
241-
signed_headers,
242-
content_hash,
243-
)
244-
245-
canonical_hash = hashlib.sha256(canonical_form.encode("utf-8")).hexdigest()
246-
# Create and sign "String to sign"
247-
string_to_sign = "RAI01-ED25519-SHA256\n{}\n{}\n{}".format(
248-
signature_date, scope, canonical_hash
249-
)
250-
251-
seed = base64.b64decode(ctx.credentials.pkey)
252-
signing_key = ed25519.SigningKey(seed)
253-
sig = signing_key.sign(string_to_sign.encode("utf-8")).hex()
254-
255-
req.headers["authorization"] = (
256-
"RAI01-ED25519-SHA256 Credential={}/{},"
257-
"SignedHeaders={},"
258-
"Signature={}".format(ctx.credentials.akey, scope, signed_headers, sig)
259-
)
260-
261-
262197
# Authenticate the request by signing or adding access token.
263198
def _authenticate(ctx: Context, req: Request) -> Request:
264199
creds = ctx.credentials
@@ -268,9 +203,6 @@ def _authenticate(ctx: Context, req: Request) -> Request:
268203
access_token = _get_access_token(ctx, req.full_url)
269204
req.headers["authorization"] = f"Bearer {access_token}"
270205
return req
271-
if isinstance(creds, AccessKeyCredentials):
272-
_sign(ctx, req)
273-
return req
274206
raise Exception("unknown credential type")
275207

276208

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
ed25519==1.5
21
grpcio-tools==1.47.0
32
protobuf==3.20.2
43
pyarrow==6.0.1

0 commit comments

Comments
 (0)