mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 15:39:58 +00:00
TUN-5915: New cloudflared command to allow to retrieve the token credentials for a Tunnel
This commit is contained in:
@@ -72,13 +72,18 @@ class NamedTunnelConfig(NamedTunnelBaseConfig):
|
||||
|
||||
return config
|
||||
|
||||
def get_token(self):
|
||||
with open(self.credentials_file) as json_file:
|
||||
creds = json.load(json_file)
|
||||
token_dict = {"a": creds["AccountTag"], "t": creds["TunnelID"], "s": creds["TunnelSecret"]}
|
||||
token_json_str = json.dumps(token_dict)
|
||||
def get_tunnel_id(self):
|
||||
return self.full_config["tunnel"]
|
||||
|
||||
return base64.b64encode(token_json_str.encode('utf-8'))
|
||||
def get_token(self):
|
||||
creds = self.get_credentials_json()
|
||||
token_dict = {"a": creds["AccountTag"], "t": creds["TunnelID"], "s": creds["TunnelSecret"]}
|
||||
token_json_str = json.dumps(token_dict)
|
||||
return base64.b64encode(token_json_str.encode('utf-8'))
|
||||
|
||||
def get_credentials_json(self):
|
||||
with open(self.credentials_file) as json_file:
|
||||
return json.load(json_file)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
35
component-tests/test_token.py
Normal file
35
component-tests/test_token.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import base64
|
||||
import json
|
||||
|
||||
from setup import get_config_from_file, persist_origin_cert
|
||||
from util import start_cloudflared
|
||||
|
||||
|
||||
class TestToken:
|
||||
def test_get_token(self, tmp_path, component_tests_config):
|
||||
config = component_tests_config()
|
||||
tunnel_id = config.get_tunnel_id()
|
||||
|
||||
token_args = ["--origincert", cert_path(), "token", tunnel_id]
|
||||
output = start_cloudflared(tmp_path, config, token_args)
|
||||
|
||||
assert parse_token(config.get_token()) == parse_token(output.stdout)
|
||||
|
||||
def test_get_credentials_file(self, tmp_path, component_tests_config):
|
||||
config = component_tests_config()
|
||||
tunnel_id = config.get_tunnel_id()
|
||||
|
||||
cred_file = tmp_path / "test_get_credentials_file.json"
|
||||
token_args = ["--origincert", cert_path(), "token", "--cred-file", cred_file, tunnel_id]
|
||||
start_cloudflared(tmp_path, config, token_args)
|
||||
|
||||
with open(cred_file) as json_file:
|
||||
assert config.get_credentials_json() == json.load(json_file)
|
||||
|
||||
|
||||
def cert_path():
|
||||
return get_config_from_file()["origincert"]
|
||||
|
||||
|
||||
def parse_token(token):
|
||||
return json.loads(base64.b64decode(token))
|
Reference in New Issue
Block a user