Skip to content

Commit

Permalink
Make parsed Keyset type public
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Apr 11, 2021
1 parent 6a0d076 commit dc77313
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 100 deletions.
14 changes: 14 additions & 0 deletions upup/pkg/fi/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ type KeystoreItem struct {
Data []byte
}

// Keyset is a parsed api.Keyset
type Keyset struct {
LegacyFormat bool
Items map[string]*KeysetItem
Primary *KeysetItem
}

// KeysetItem is a certificate/key pair in a Keyset.
type KeysetItem struct {
Id string
Certificate *pki.Certificate
PrivateKey *pki.PrivateKey
}

// Keystore contains just the functions we need to issue keypairs, not to list / manage them
type Keystore interface {
// FindKeypair finds a cert & private key, returning nil where either is not found
Expand Down
60 changes: 23 additions & 37 deletions upup/pkg/fi/clientset_castore.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,58 +65,44 @@ func NewClientsetSSHCredentialStore(cluster *kops.Cluster, clientset kopsinterna
return c
}

// keyset is a parsed Keyset
type keyset struct {
legacyFormat bool
items map[string]*keysetItem
primary *keysetItem
}

// keysetItem is a parsed KeysetItem
type keysetItem struct {
id string
certificate *pki.Certificate
privateKey *pki.PrivateKey
}

func parseKeyset(o *kops.Keyset) (*keyset, error) {
func parseKeyset(o *kops.Keyset) (*Keyset, error) {
name := o.Name

keyset := &keyset{
items: make(map[string]*keysetItem),
keyset := &Keyset{
Items: make(map[string]*KeysetItem),
}

for _, key := range o.Spec.Keys {
ki := &keysetItem{
id: key.Id,
ki := &KeysetItem{
Id: key.Id,
}
if len(key.PublicMaterial) != 0 {
cert, err := pki.ParsePEMCertificate(key.PublicMaterial)
if err != nil {
klog.Warningf("key public material was %s", key.PublicMaterial)
return nil, fmt.Errorf("error loading certificate %s/%s: %v", name, key.Id, err)
}
ki.certificate = cert
ki.Certificate = cert
}

if len(key.PrivateMaterial) != 0 {
privateKey, err := pki.ParsePEMPrivateKey(key.PrivateMaterial)
if err != nil {
return nil, fmt.Errorf("error loading private key %s/%s: %v", name, key.Id, err)
}
ki.privateKey = privateKey
ki.PrivateKey = privateKey
}

keyset.items[key.Id] = ki
keyset.Items[key.Id] = ki
}

keyset.primary = keyset.items[FindPrimary(o).Id]
keyset.Primary = keyset.Items[FindPrimary(o).Id]

return keyset, nil
}

// loadKeyset gets the named keyset and the format of the Keyset.
func (c *ClientsetCAStore) loadKeyset(ctx context.Context, name string) (*keyset, error) {
// loadKeyset gets the named Keyset and the format of the Keyset.
func (c *ClientsetCAStore) loadKeyset(ctx context.Context, name string) (*Keyset, error) {
o, err := c.clientset.Keysets(c.namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
Expand Down Expand Up @@ -167,8 +153,8 @@ func (c *ClientsetCAStore) FindKeypair(name string) (*pki.Certificate, *pki.Priv
return nil, nil, false, err
}

if keyset != nil && keyset.primary != nil {
return keyset.primary.certificate, keyset.primary.privateKey, keyset.legacyFormat, nil
if keyset != nil && keyset.Primary != nil {
return keyset.Primary.Certificate, keyset.Primary.PrivateKey, keyset.LegacyFormat, nil
}

return nil, nil, false, nil
Expand All @@ -182,8 +168,8 @@ func (c *ClientsetCAStore) FindCert(name string) (*pki.Certificate, error) {
return nil, err
}

if keyset != nil && keyset.primary != nil {
return keyset.primary.certificate, nil
if keyset != nil && keyset.Primary != nil {
return keyset.Primary.Certificate, nil
}

return nil, nil
Expand All @@ -200,15 +186,15 @@ func (c *ClientsetCAStore) FindCertificatePool(name string) (*CertificatePool, e
pool := &CertificatePool{}

if keyset != nil {
if keyset.primary != nil {
pool.Primary = keyset.primary.certificate
if keyset.Primary != nil {
pool.Primary = keyset.Primary.Certificate
}

for id, item := range keyset.items {
if id == keyset.primary.id {
for id, item := range keyset.Items {
if id == keyset.Primary.Id {
continue
}
pool.Secondary = append(pool.Secondary, item.certificate)
pool.Secondary = append(pool.Secondary, item.Certificate)
}
}
return pool, nil
Expand Down Expand Up @@ -305,8 +291,8 @@ func (c *ClientsetCAStore) FindPrivateKey(name string) (*pki.PrivateKey, error)
return nil, err
}

if keyset != nil && keyset.primary != nil {
return keyset.primary.privateKey, nil
if keyset != nil && keyset.Primary != nil {
return keyset.Primary.PrivateKey, nil
}
return nil, nil
}
Expand Down Expand Up @@ -355,7 +341,7 @@ func (c *ClientsetCAStore) addKey(ctx context.Context, name string, keysetType k
return nil
}

// deleteKeysetItem deletes the specified key from the registry; deleting the whole keyset if it was the last one
// deleteKeysetItem deletes the specified key from the registry; deleting the whole Keyset if it was the last one.
func deleteKeysetItem(client kopsinternalversion.KeysetInterface, name string, keysetType kops.KeysetType, id string) error {
ctx := context.TODO()

Expand Down
Loading

0 comments on commit dc77313

Please sign in to comment.