Debian Codegraph @

token/worker/client.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package worker
18
19 import (
20 "bytes"
21 "context"
22 "crypto"
23 "crypto/rsa"
24 "crypto/x509"
25 "encoding/json"
26 "io"
27 "net/http"
28 "net/url"
29 "time"
30
31 "github.com/sassoftware/relic/v7/config"
32 "github.com/sassoftware/relic/v7/internal/workerrpc"
33 "github.com/sassoftware/relic/v7/token"
34 )
35
36 const (
37 defaultRetries = 5
38 defaultTimeout = 60 * time.Second
39
40 tokenType = "worker"
41 )
42
43 func (t *WorkerToken) request(ctx context.Context, path string, rr workerrpc.Request) (*workerrpc.Response, error) {
44 req := &http.Request{
45 Method: http.MethodPost,
46 URL: &url.URL{Scheme: "http", Host: t.addr, Path: path},
47 Header: http.Header{"Auth-Cookie": []string{t.cookie}},
48 }
49 blob, err := json.Marshal(rr)
50 if err != nil {
51 return nil, err
52 }
53 req.GetBody = func() (io.ReadCloser, error) {
54 return io.NopCloser(bytes.NewReader(blob)), nil
55 }
56 return t.doRetry(req.WithContext(ctx))
57 }
58
59 func (t *WorkerToken) Ping(ctx context.Context) error {
60 _, err := t.request(ctx, workerrpc.Ping, workerrpc.Request{})
61 return err
62 }
63
64 func (t *WorkerToken) Config() *config.TokenConfig {
65 return t.tconf
66 }
67
68 func (t *WorkerToken) GetKey(ctx context.Context, keyName string) (token.Key, error) {
69 kconf, err := t.config.GetKey(keyName)
70 if err != nil {
71 return nil, err
72 }
73 res, err := t.request(ctx, workerrpc.GetKey, workerrpc.Request{KeyName: kconf.Name()})
74 if err != nil {
75 return nil, err
76 }
77 pub, err := x509.ParsePKIXPublicKey(res.Value)
78 if err != nil {
79 return nil, err
80 }
81 return &workerKey{
82 token: t,
83 kconf: kconf,
84 public: pub,
85 id: res.ID,
86 cert: res.Cert,
87 }, nil
88 }
89
90 func (t *WorkerToken) Import(keyName string, privKey crypto.PrivateKey) (token.Key, error) {
91 return nil, token.NotImplementedError{Op: "import-key", Type: tokenType}
92 }
93
94 func (t *WorkerToken) ImportCertificate(cert *x509.Certificate, labelBase string) error {
95 return token.NotImplementedError{Op: "import-certificate", Type: tokenType}
96 }
97
98 func (t *WorkerToken) Generate(keyName string, keyType token.KeyType, bits uint) (token.Key, error) {
99 return nil, token.NotImplementedError{Op: "generate-key", Type: tokenType}
100 }
101
102 func (t *WorkerToken) ListKeys(opts token.ListOptions) error {
103 return token.NotImplementedError{Op: "list-keys", Type: tokenType}
104 }
105
106 type workerKey struct {
107 token *WorkerToken
108 kconf *config.KeyConfig
109 public crypto.PublicKey
110 id []byte
111 cert []byte
112 }
113
114 func (k *workerKey) Config() *config.KeyConfig {
115 return k.kconf
116 }
117
118 func (k *workerKey) Certificate() []byte {
119 return k.cert
120 }
121
122 func (k *workerKey) Public() crypto.PublicKey {
123 return k.public
124 }
125
126 func (k *workerKey) GetID() []byte {
127 return k.id
128 }
129
130 func (k *workerKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
131 return k.SignContext(context.Background(), digest, opts)
132 }
133
134 func (k *workerKey) SignContext(ctx context.Context, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
135 rr := workerrpc.Request{
136 KeyName: k.kconf.Name(),
137 KeyID: k.id,
138 Digest: digest,
139 }
140 if opts != nil {
141 rr.Hash = uint(opts.HashFunc())
142 if o, ok := opts.(*rsa.PSSOptions); ok {
143 rr.SaltLength = &o.SaltLength
144 }
145 }
146 res, err := k.token.request(ctx, workerrpc.Sign, rr)
147 if err != nil {
148 return nil, err
149 }
150 return res.Value, nil
151 }
152
153 func (k *workerKey) ImportCertificate(cert *x509.Certificate) error {
154 return token.NotImplementedError{Op: "import-certificate", Type: tokenType}
155 }