-
Notifications
You must be signed in to change notification settings - Fork 9
/
certificates.go
57 lines (48 loc) · 1.74 KB
/
certificates.go
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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package nsingress
import (
"context"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/internal/tools/maketlscert"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/types"
)
func AllocateDomainCertificate(ctx context.Context, env *schema.Environment, entry *schema.Stack_Entry, template *schema.Domain) (*schema.Certificate, error) {
if env.Purpose == schema.Environment_PRODUCTION {
return allocateName(ctx, entry.Server, fnapi.AllocateOpts{
FQDN: template.Fqdn,
// XXX remove org -- it should be parsed from fqdn.
Org: entry.ServerNaming.GetWithOrg(),
})
} else {
bundle, err := maketlscert.CreateSelfSignedCertificateChain(ctx, env, &types.TLSCertificateSpec{
Description: entry.Server.PackageName,
DnsName: []string{template.Fqdn},
})
if err != nil {
return nil, err
}
return bundle.Server, nil
}
}
func allocateName(ctx context.Context, srv runtime.Deployable, opts fnapi.AllocateOpts) (*schema.Certificate, error) {
opts.NoTLS = runtime.NamingNoTLS
opts.Scope = srv.GetPackageRef().AsPackageName()
nr, err := fnapi.AllocateName(ctx, opts)
if err != nil {
return nil, err
}
return certFromResource(nr), nil
}
func certFromResource(res *fnapi.NameResource) *schema.Certificate {
if res.Certificate.PrivateKey != nil && res.Certificate.CertificateBundle != nil {
return &schema.Certificate{
PrivateKey: res.Certificate.PrivateKey,
CertificateBundle: res.Certificate.CertificateBundle,
}
}
return nil
}