Skip to content

Commit 078996d

Browse files
author
Aiden Keating
committed
Include certificate pinning hashes in the service configuration
This change allows the SDKs to consume hashes used for certificate pinning. This means that the end user does not have to configure certificate pinning manually or using a IDE to have pinning with their provisioned services.
1 parent 5eec520 commit 078996d

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

pkg/cmd/clientConfig.go

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ kubectl plugin mobile get clientconfig`,
120120
}
121121
}
122122
if includedService {
123+
err = appendCertificatePinningInfoToService(svcConfig)
124+
if err != nil {
125+
return errors.Wrap(err, "unable to append certificate pinning information to service config")
126+
}
123127
ret = append(ret, svcConfig)
124128
}
125129
}

pkg/cmd/convert.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,40 @@ package cmd
1616

1717
import (
1818
"strings"
19-
2019
"k8s.io/client-go/pkg/api/v1"
20+
"crypto/sha256"
21+
"crypto/tls"
22+
"encoding/base64"
23+
"net/url"
2124
)
2225

2326
func isClientConfigKey(key string) bool {
2427
return key == "url" || key == "name" || key == "type" || key == "id"
2528
}
2629

30+
func appendCertificatePinningInfoToService(s *ServiceConfig) error {
31+
serviceURL, err := url.Parse(s.URL)
32+
if err != nil {
33+
return err
34+
}
35+
if serviceURL.Scheme != "https" {
36+
return nil
37+
}
38+
// TODO: Make the InsecureSkipVerify here configurable. I think there will be times when we don't want to allow auto-pinning to unverified certificates.
39+
// TODO: Allow for the Host variable to contain a port. So split it and then if there's a port use that, else use 443.
40+
conn, err := tls.Dial("tcp", serviceURL.Host+":443", &tls.Config{
41+
InsecureSkipVerify: true,
42+
})
43+
if err != nil {
44+
return err
45+
}
46+
hasher := sha256.New()
47+
// TODO: Do we want to loop through here? The command here https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning only returns what we are currently returning.
48+
hasher.Write(conn.ConnectionState().PeerCertificates[0].RawSubjectPublicKeyInfo)
49+
s.CertificatePinningHashes = []string{base64.StdEncoding.EncodeToString(hasher.Sum(nil))}
50+
return nil
51+
}
52+
2753
func convertSecretToMobileService(s v1.Secret) *Service {
2854
params := map[string]string{}
2955
for key, value := range s.Data {
@@ -32,6 +58,7 @@ func convertSecretToMobileService(s v1.Secret) *Service {
3258
}
3359
}
3460
external := s.Labels["external"] == "true"
61+
3562
return &Service{
3663
Namespace: s.Labels["namespace"],
3764
ID: s.Name,

pkg/cmd/types.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ type ServiceConfigs struct {
8080

8181
//ServiceConfig is the configuration for a specific service
8282
type ServiceConfig struct {
83-
ID string `json:"id"`
84-
Name string `json:"name"`
85-
Type string `json:"type"`
86-
URL string `json:"url"`
87-
Config map[string]interface{} `json:"config"`
83+
ID string `json:"id"`
84+
Name string `json:"name"`
85+
Type string `json:"type"`
86+
URL string `json:"url"`
87+
Config map[string]interface{} `json:"config"`
88+
CertificatePinningHashes []string `json:"certificatePinningHashes"`
8889
}
8990

9091
// defaultSecretConvertor will provide a default secret to config conversion

0 commit comments

Comments
 (0)