Skip to content

Commit 63833b0

Browse files
committed
AUTH-3455: Generate short-lived ssh cert per hostname
1 parent da4d0b2 commit 63833b0

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

cmd/cloudflared/access/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func sshGen(c *cli.Context) error {
387387
return err
388388
}
389389

390-
if err := sshgen.GenerateShortLivedCertificate(appInfo, cfdToken); err != nil {
390+
if err := sshgen.GenerateShortLivedCertificate(originURL, cfdToken); err != nil {
391391
return err
392392
}
393393

sshgen/sshgen.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"io"
1313
"io/ioutil"
1414
"net/http"
15+
"net/url"
1516
"time"
1617

1718
"github.com/coreos/go-oidc/jose"
@@ -51,8 +52,8 @@ type errorResponse struct {
5152
var mockRequest func(url, contentType string, body io.Reader) (*http.Response, error) = nil
5253

5354
// GenerateShortLivedCertificate generates and stores a keypair for short lived certs
54-
func GenerateShortLivedCertificate(appInfo *cfpath.AppInfo, token string) error {
55-
fullName, err := cfpath.GenerateAppTokenFilePathFromURL(appInfo.AppDomain, appInfo.AppAUD, keyName)
55+
func GenerateShortLivedCertificate(appURL *url.URL, token string) error {
56+
fullName, err := cfpath.GenerateSSHCertFilePathFromURL(appURL, keyName)
5657
if err != nil {
5758
return err
5859
}

sshgen/sshgen_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"io/ioutil"
1010
"net/http"
1111
"net/http/httptest"
12+
"net/url"
1213
"os"
14+
"strings"
1315
"testing"
1416
"time"
1517

@@ -32,11 +34,12 @@ type signingArguments struct {
3234
}
3335

3436
func TestCertGenSuccess(t *testing.T) {
35-
appInfo := &cfpath.AppInfo{AppAUD: "abcd1234", AppDomain: "mySite.com"}
37+
url, _ := url.Parse("https://cf-test-access.com/testpath")
3638
token := tokenGenerator()
3739

38-
fullName, err := cfpath.GenerateAppTokenFilePathFromURL(appInfo.AppDomain, appInfo.AppAUD, keyName)
40+
fullName, err := cfpath.GenerateSSHCertFilePathFromURL(url, keyName)
3941
assert.NoError(t, err)
42+
assert.True(t, strings.HasSuffix(fullName, "/cf-test-access.com-testpath-cf_key"))
4043

4144
pubKeyName := fullName + ".pub"
4245
certKeyName := fullName + "-cert.pub"
@@ -65,7 +68,7 @@ func TestCertGenSuccess(t *testing.T) {
6568
return w.Result(), nil
6669
}
6770

68-
err = GenerateShortLivedCertificate(appInfo, token)
71+
err = GenerateShortLivedCertificate(url, token)
6972
assert.NoError(t, err)
7073

7174
exist, err := config.FileExists(fullName)

token/path.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package token
22

33
import (
44
"fmt"
5+
"net/url"
56
"os"
67
"path/filepath"
78
"strings"
@@ -11,6 +12,16 @@ import (
1112
"github.com/cloudflare/cloudflared/config"
1213
)
1314

15+
// GenerateSSHCertFilePathFromURL will return a file path for creating short lived certificates
16+
func GenerateSSHCertFilePathFromURL(url *url.URL, suffix string) (string, error) {
17+
configPath, err := getConfigPath()
18+
if err != nil {
19+
return "", err
20+
}
21+
name := strings.Replace(fmt.Sprintf("%s%s-%s", url.Hostname(), url.EscapedPath(), suffix), "/", "-", -1)
22+
return filepath.Join(configPath, name), nil
23+
}
24+
1425
// GenerateAppTokenFilePathFromURL will return a filepath for given Access org token
1526
func GenerateAppTokenFilePathFromURL(appDomain, aud string, suffix string) (string, error) {
1627
configPath, err := getConfigPath()

0 commit comments

Comments
 (0)