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

Feat: Cloud connection arguments #49

Merged
merged 5 commits into from
Jun 5, 2023
Merged

Conversation

quinchs
Copy link
Collaborator

@quinchs quinchs commented May 19, 2023

Summary

This PR adds support for connecting to cloud instances, in short:

  • EdgeDBConnection.Parse resolves cloud instance names from both instance and dsn parameters.
  • EdgeDBBinaryClient will send secret_key parameter in the ClientHandshake message if its present in the EdgeDBConnection.
  • Cloud instance name parsing is handled by this method:
    private void ParseCloudInstanceName(string name)
    {
    if(name.Length > DOMAIN_NAME_MAX_LEN)
    {
    throw new ConfigurationException($"Cloud instance name must be {DOMAIN_NAME_MAX_LEN} characters or less");
    }
    string? secretKey = SecretKey;
    if(secretKey is null)
    {
    var profile = ConfigUtils.ReadCloudProfile(CloudProfile);
    if (profile.SecretKey is null)
    {
    throw new ConfigurationException("Secret key cannot be null");
    }
    secretKey = profile.SecretKey;
    }
    var spl = secretKey.Split('.');
    if(spl.Length < 2)
    {
    throw new ConfigurationException("Invalid secret key: does not contain payload");
    }
    var json = Convert.FromBase64String(spl[1]);
    var jsonData = JsonConvert.DeserializeObject<Dictionary<string, object?>>(Encoding.UTF8.GetString(json))!;
    if(!jsonData.TryGetValue("iss", out var dnsZone))
    {
    throw new ConfigurationException("Invalid secret key: payload does not contain 'iss' value");
    }
    // safe: checks name length above to be less than DOMAIN_NAME_MAX_LEN
    Span<byte> instanceNameBuffer = stackalloc byte[name.Length];
    Encoding.UTF8.GetBytes(name, instanceNameBuffer);
    var dnsBucket = (CRCHQX.CRCHqx(instanceNameBuffer, 0) % 100)
    .ToString()
    .PadLeft(2, '0');
    spl = name.Split("/");
    Hostname = $"{spl[1]}--{spl[0]}.c-{dnsBucket}.i.{dnsZone}";
    SecretKey ??= secretKey;
    }

Copy link
Member

@fantix fantix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great in general! Just missing parsing the cloud-profile file in project stash dir.

@quinchs quinchs merged commit 254aa58 into dev Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants