Skip to content

Commit 19106cd

Browse files
committed
TUN-6935: Cloudflared should use APIToken instead of serviceKey
This commit makes cloudflared use the API token provided during login instead of service key. In addition, it eliminates some of the old formats since those are legacy and we only support cloudflared versions newer than 6 months.
1 parent b50f172 commit 19106cd

File tree

10 files changed

+33
-264
lines changed

10 files changed

+33
-264
lines changed

certutil/certutil.go

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
package certutil
22

33
import (
4-
"crypto/x509"
54
"encoding/json"
65
"encoding/pem"
76
"fmt"
8-
"strings"
97
)
108

119
type namedTunnelToken struct {
12-
ZoneID string `json:"zoneID"`
13-
AccountID string `json:"accountID"`
14-
ServiceKey string `json:"serviceKey"`
10+
ZoneID string `json:"zoneID"`
11+
AccountID string `json:"accountID"`
12+
APIToken string `json:"apiToken"`
1513
}
1614

1715
type OriginCert struct {
18-
PrivateKey interface{}
19-
Cert *x509.Certificate
20-
ZoneID string
21-
ServiceKey string
22-
AccountID string
16+
ZoneID string
17+
APIToken string
18+
AccountID string
2319
}
2420

2521
func DecodeOriginCert(blocks []byte) (*OriginCert, error) {
@@ -33,60 +29,28 @@ func DecodeOriginCert(blocks []byte) (*OriginCert, error) {
3329
break
3430
}
3531
switch block.Type {
36-
case "PRIVATE KEY":
37-
if originCert.PrivateKey != nil {
38-
return nil, fmt.Errorf("Found multiple private key in the certificate")
39-
}
40-
// RSA private key
41-
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
42-
if err != nil {
43-
return nil, fmt.Errorf("Cannot parse private key")
44-
}
45-
originCert.PrivateKey = privateKey
46-
case "CERTIFICATE":
47-
if originCert.Cert != nil {
48-
return nil, fmt.Errorf("Found multiple certificates in the certificate")
49-
}
50-
cert, err := x509.ParseCertificates(block.Bytes)
51-
if err != nil {
52-
return nil, fmt.Errorf("Cannot parse certificate")
53-
} else if len(cert) > 1 {
54-
return nil, fmt.Errorf("Found multiple certificates in the certificate")
55-
}
56-
originCert.Cert = cert[0]
57-
case "WARP TOKEN", "ARGO TUNNEL TOKEN":
58-
if originCert.ZoneID != "" || originCert.ServiceKey != "" {
32+
case "PRIVATE KEY", "CERTIFICATE":
33+
// this is for legacy purposes.
34+
break
35+
case "ARGO TUNNEL TOKEN":
36+
if originCert.ZoneID != "" || originCert.APIToken != "" {
5937
return nil, fmt.Errorf("Found multiple tokens in the certificate")
6038
}
6139
// The token is a string,
6240
// Try the newer JSON format
6341
ntt := namedTunnelToken{}
6442
if err := json.Unmarshal(block.Bytes, &ntt); err == nil {
6543
originCert.ZoneID = ntt.ZoneID
66-
originCert.ServiceKey = ntt.ServiceKey
44+
originCert.APIToken = ntt.APIToken
6745
originCert.AccountID = ntt.AccountID
68-
} else {
69-
// Try the older format, where the zoneID and service key are separated by
70-
// a new line character
71-
token := string(block.Bytes)
72-
s := strings.Split(token, "\n")
73-
if len(s) != 2 {
74-
return nil, fmt.Errorf("Cannot parse token")
75-
}
76-
originCert.ZoneID = s[0]
77-
originCert.ServiceKey = s[1]
7846
}
7947
default:
8048
return nil, fmt.Errorf("Unknown block %s in the certificate", block.Type)
8149
}
8250
block, rest = pem.Decode(rest)
8351
}
8452

85-
if originCert.PrivateKey == nil {
86-
return nil, fmt.Errorf("Missing private key in the certificate")
87-
} else if originCert.Cert == nil {
88-
return nil, fmt.Errorf("Missing certificate in the certificate")
89-
} else if originCert.ZoneID == "" || originCert.ServiceKey == "" {
53+
if originCert.ZoneID == "" || originCert.APIToken == "" {
9054
return nil, fmt.Errorf("Missing token in the certificate")
9155
}
9256

certutil/certutil_test.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,39 @@ func TestLoadOriginCert(t *testing.T) {
1313
assert.Equal(t, fmt.Errorf("Cannot decode empty certificate"), err)
1414
assert.Nil(t, cert)
1515

16-
blocks, err := ioutil.ReadFile("test-cert-no-key.pem")
17-
assert.Nil(t, err)
18-
cert, err = DecodeOriginCert(blocks)
19-
assert.Equal(t, fmt.Errorf("Missing private key in the certificate"), err)
20-
assert.Nil(t, cert)
21-
22-
blocks, err = ioutil.ReadFile("test-cert-two-certificates.pem")
23-
assert.Nil(t, err)
24-
cert, err = DecodeOriginCert(blocks)
25-
assert.Equal(t, fmt.Errorf("Found multiple certificates in the certificate"), err)
26-
assert.Nil(t, cert)
27-
28-
blocks, err = ioutil.ReadFile("test-cert-unknown-block.pem")
16+
blocks, err := ioutil.ReadFile("test-cert-unknown-block.pem")
2917
assert.Nil(t, err)
3018
cert, err = DecodeOriginCert(blocks)
3119
assert.Equal(t, fmt.Errorf("Unknown block RSA PRIVATE KEY in the certificate"), err)
3220
assert.Nil(t, cert)
21+
}
3322

34-
blocks, err = ioutil.ReadFile("test-cert.pem")
23+
func TestJSONArgoTunnelTokenEmpty(t *testing.T) {
24+
cert, err := DecodeOriginCert([]byte{})
25+
blocks, err := ioutil.ReadFile("test-cert-no-token.pem")
3526
assert.Nil(t, err)
3627
cert, err = DecodeOriginCert(blocks)
37-
assert.Nil(t, err)
38-
assert.NotNil(t, cert)
39-
assert.Equal(t, "7b0a4d77dfb881c1a3b7d61ea9443e19", cert.ZoneID)
40-
key := "v1.0-58bd4f9e28f7b3c28e05a35ff3e80ab4fd9644ef3fece537eb0d12e2e9258217-183442fbb0bbdb3e571558fec9b5589ebd77aafc87498ee3f09f64a4ad79ffe8791edbae08b36c1d8f1d70a8670de56922dff92b15d214a524f4ebfa1958859e-7ce80f79921312a6022c5d25e2d380f82ceaefe3fbdc43dd13b080e3ef1e26f7"
41-
assert.Equal(t, key, cert.ServiceKey)
42-
}
43-
44-
func TestNewlineArgoTunnelToken(t *testing.T) {
45-
ArgoTunnelTokenTest(t, "test-argo-tunnel-cert.pem")
28+
assert.Equal(t, fmt.Errorf("Missing token in the certificate"), err)
29+
assert.Nil(t, cert)
4630
}
4731

4832
func TestJSONArgoTunnelToken(t *testing.T) {
4933
// The given cert's Argo Tunnel Token was generated by base64 encoding this JSON:
5034
// {
5135
// "zoneID": "7b0a4d77dfb881c1a3b7d61ea9443e19",
52-
// "serviceKey": "test-service-key",
36+
// "apiToken": "test-service-key",
5337
// "accountID": "abcdabcdabcdabcd1234567890abcdef"
5438
// }
55-
ArgoTunnelTokenTest(t, "test-argo-tunnel-cert-json.pem")
39+
CloudflareTunnelTokenTest(t, "test-cloudflare-tunnel-cert-json.pem")
5640
}
5741

58-
func ArgoTunnelTokenTest(t *testing.T, path string) {
42+
func CloudflareTunnelTokenTest(t *testing.T, path string) {
5943
blocks, err := ioutil.ReadFile(path)
6044
assert.Nil(t, err)
6145
cert, err := DecodeOriginCert(blocks)
6246
assert.Nil(t, err)
6347
assert.NotNil(t, cert)
6448
assert.Equal(t, "7b0a4d77dfb881c1a3b7d61ea9443e19", cert.ZoneID)
6549
key := "test-service-key"
66-
assert.Equal(t, key, cert.ServiceKey)
50+
assert.Equal(t, key, cert.APIToken)
6751
}

certutil/test-cert-no-key.pem

Lines changed: 0 additions & 33 deletions
This file was deleted.

certutil/test-argo-tunnel-cert-json.pem renamed to certutil/test-cert-no-token.pem

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ K5rShE/l+90YAOzHC89OH/wUz3I5KYOFuehoAiEA8e92aIf9XBkr0K6EvFCiSsD+
5151
x+Yo/cL8fGfVpPt4UM8=
5252
-----END CERTIFICATE-----
5353
-----BEGIN ARGO TUNNEL TOKEN-----
54-
eyJ6b25lSUQiOiAiN2IwYTRkNzdkZmI4ODFjMWEzYjdkNjFlYTk0NDNlMTkiLCAi
55-
c2VydmljZUtleSI6ICJ0ZXN0LXNlcnZpY2Uta2V5IiwgImFjY291bnRJRCI6ICJh
56-
YmNkYWJjZGFiY2RhYmNkMTIzNDU2Nzg5MGFiY2RlZiJ9
54+
eyJ6b25lSUQiOiAiN2IwYTRkNzdkZmI4ODFjMWEzYjdkNjFlYTk0NDNlMTkiLCAiYWNjb3VudElE
55+
IjogImFiY2RhYmNkYWJjZGFiY2QxMjM0NTY3ODkwYWJjZGVmIn0=
5756
-----END ARGO TUNNEL TOKEN-----

certutil/test-cert-two-certificates.pem

Lines changed: 0 additions & 85 deletions
This file was deleted.

certutil/test-cert-unknown-block.pem

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ cmUuY29tL29yaWdpbl9lY2NfY2EuY3JsMAoGCCqGSM49BAMCA0gAMEUCIDV7HoMj
5050
K5rShE/l+90YAOzHC89OH/wUz3I5KYOFuehoAiEA8e92aIf9XBkr0K6EvFCiSsD+
5151
x+Yo/cL8fGfVpPt4UM8=
5252
-----END CERTIFICATE-----
53-
-----BEGIN WARP TOKEN-----
53+
-----BEGIN ARGO TUNNEL TOKEN-----
5454
N2IwYTRkNzdkZmI4ODFjMWEzYjdkNjFlYTk0NDNlMTkKdjEuMC01OGJkNGY5ZTI4
5555
ZjdiM2MyOGUwNWEzNWZmM2U4MGFiNGZkOTY0NGVmM2ZlY2U1MzdlYjBkMTJlMmU5
5656
MjU4MjE3LTE4MzQ0MmZiYjBiYmRiM2U1NzE1NThmZWM5YjU1ODllYmQ3N2FhZmM4
5757
NzQ5OGVlM2YwOWY2NGE0YWQ3OWZmZTg3OTFlZGJhZTA4YjM2YzFkOGYxZDcwYTg2
5858
NzBkZTU2OTIyZGZmOTJiMTVkMjE0YTUyNGY0ZWJmYTE5NTg4NTllLTdjZTgwZjc5
5959
OTIxMzEyYTYwMjJjNWQyNWUyZDM4MGY4MmNlYWVmZTNmYmRjNDNkZDEzYjA4MGUz
6060
ZWYxZTI2Zjc=
61-
-----END WARP TOKEN-----
61+
-----END ARGO TUNNEL TOKEN-----
6262
-----BEGIN RSA PRIVATE KEY-----
6363
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCfGswL16Fz9Ei3
6464
sAg5AmBizoN2nZdyXHP8T57UxUMcrlJXEEXCVS5RR4m9l+EmK0ng6yHR1H5oX1Lg

certutil/test-cert.pem

Lines changed: 0 additions & 61 deletions
This file was deleted.

certutil/test-argo-tunnel-cert.pem renamed to certutil/test-cloudflare-tunnel-cert-json.pem

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ K5rShE/l+90YAOzHC89OH/wUz3I5KYOFuehoAiEA8e92aIf9XBkr0K6EvFCiSsD+
5151
x+Yo/cL8fGfVpPt4UM8=
5252
-----END CERTIFICATE-----
5353
-----BEGIN ARGO TUNNEL TOKEN-----
54-
N2IwYTRkNzdkZmI4ODFjMWEzYjdkNjFlYTk0NDNlMTkKdGVzdC1zZXJ2aWNlLWtl
55-
eQ==
54+
eyJ6b25lSUQiOiAiN2IwYTRkNzdkZmI4ODFjMWEzYjdkNjFlYTk0NDNlMTkiLCAiYXBpVG9rZW4i
55+
OiAidGVzdC1zZXJ2aWNlLWtleSIsICJhY2NvdW50SUQiOiAiYWJjZGFiY2RhYmNkYWJjZDEyMzQ1
56+
Njc4OTBhYmNkZWYifQ==
5657
-----END ARGO TUNNEL TOKEN-----

cfapi/base_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (r *RESTClient) sendRequest(method string, url url.URL, body interface{}) (
104104
if bodyReader != nil {
105105
req.Header.Set("Content-Type", jsonContentType)
106106
}
107-
req.Header.Add("X-Auth-User-Service-Key", r.authToken)
107+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", r.authToken))
108108
req.Header.Add("Accept", "application/json;version=1")
109109
return r.client.Do(req)
110110
}

0 commit comments

Comments
 (0)