-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor Identities API Instead of providing raw strings that are up to the client to determine how to parse, return typed structs. Encoded keys are included and should be parsed based on the returned type. Signed-off-by: Hayden Blauzvern <[email protected]> * Remove debug statement Signed-off-by: Hayden Blauzvern <[email protected]> --------- Signed-off-by: Hayden Blauzvern <[email protected]>
- Loading branch information
1 parent
d954fef
commit e7b377a
Showing
18 changed files
with
343 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2023 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package identity | ||
|
||
type Identity struct { | ||
// Types include: *rsa.PublicKey, *ecdsa.PublicKey, ed25519.Publickey, | ||
// *x509.Certificate, openpgp.EntityList, *minisign.PublicKey, ssh.PublicKey | ||
Crypto any | ||
// Based on type of Crypto. Possible values include: PEM-encoded public key, | ||
// PEM-encoded certificate, canonicalized PGP public key, encoded Minisign | ||
// public key, encoded SSH public key | ||
Raw []byte | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
"testing" | ||
|
||
"github.com/sassoftware/relic/lib/pkcs7" | ||
"github.com/sigstore/rekor/pkg/pki/identity" | ||
"github.com/sigstore/rekor/pkg/pki/x509/testutils" | ||
"github.com/sigstore/sigstore/pkg/cryptoutils" | ||
) | ||
|
@@ -470,12 +471,12 @@ A6ydFG8HXGWcnVVIVQ== | |
{ | ||
name: "email in subject", | ||
pkcs7: pkcsPEMEmail, | ||
identities: []string{pkcsEmailCert, pkcsPEMEmailKey, "[email protected]"}, | ||
identities: []string{pkcsEmailCert, pkcsPEMEmailKey}, | ||
}, | ||
{ | ||
name: "email and URI in subject alternative name", | ||
pkcs7: string(pkcs7bytes), | ||
identities: []string{string(leafCertPEM), string(leafPEM), "[email protected]", "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/builder_go_slsa3.yml@refs/tags/v1.1.1"}, | ||
identities: []string{string(leafCertPEM), string(leafPEM)}, | ||
}, | ||
} | ||
|
||
|
@@ -485,23 +486,33 @@ A6ydFG8HXGWcnVVIVQ== | |
if err != nil { | ||
t.Fatal(err) | ||
} | ||
identities, err := pub.Identities() | ||
ids, err := pub.Identities() | ||
if err != nil { | ||
t.Fatalf("unexpected error getting identities: %v", err) | ||
} | ||
if len(ids) != 2 { | ||
t.Fatalf("expected 2 identities, got %d", len(ids)) | ||
} | ||
|
||
if len(identities) == len(tt.identities) { | ||
if len(identities) > 0 { | ||
sort.Strings(identities) | ||
sort.Strings(tt.identities) | ||
if !reflect.DeepEqual(identities, tt.identities) { | ||
t.Errorf("%v: Error getting identities from keys, got %v, expected %v", tt.name, identities, tt.identities) | ||
} | ||
} | ||
} else { | ||
t.Errorf("%v: Error getting identities from keys, got %v, expected %v", tt.name, identities, tt.identities) | ||
// compare certificate | ||
cert, _ := cryptoutils.UnmarshalCertificatesFromPEM([]byte(tt.identities[0])) | ||
expectedID := identity.Identity{Crypto: cert[0], Raw: []byte(tt.identities[0])} | ||
if !ids[0].Crypto.(*x509.Certificate).Equal(expectedID.Crypto.(*x509.Certificate)) { | ||
t.Errorf("certificates did not match") | ||
} | ||
if !reflect.DeepEqual(ids[0].Raw, expectedID.Raw) { | ||
t.Errorf("raw identities did not match, expected %v, got %v", ids[0].Raw, string(expectedID.Raw)) | ||
} | ||
|
||
// compare public key | ||
key, _ := cryptoutils.UnmarshalPEMToPublicKey([]byte(tt.identities[1])) | ||
expectedID = identity.Identity{Crypto: key, Raw: []byte(tt.identities[1])} | ||
if err := cryptoutils.EqualKeys(expectedID.Crypto, ids[1].Crypto); err != nil { | ||
t.Errorf("%v: public keys did not match: %v", tt.name, err) | ||
} | ||
if !reflect.DeepEqual(ids[1].Raw, expectedID.Raw) { | ||
t.Errorf("%v: raw identities did not match", tt.name) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,32 +17,44 @@ package ssh | |
import ( | ||
"math/rand" | ||
"reflect" | ||
"sort" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/sigstore/rekor/pkg/pki/identity" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
func TestIdentities(t *testing.T) { | ||
// from ssh_e2e_test.go | ||
publicKey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDXofkiahE7uavjWvxnwkUF27qMgz7pdTwzSv0XzVG6EtirOv3PDWct4YKoXE9c0EqbxnIfYEKwEextdvB7zkgwczdJSHxf/18jQumLn/FuoCmugVSk1H5Qli/qzwBpaTnOk3WuakGuoYUl8ZAokKKgOKLA0aZJ1WRQ2ZCZggA3EkwNZiY17y9Q6HqdgQcH6XN8aAMADNVJdMAJb33hSRJjjsAPTmzBTishP8lYDoGRSsSE7/8XRBCEV5E4I8mI9GElcZwV/1KJx98mpH8QvMzXM1idFcwPRtt1NTAOshwgUU0Fu1x8lU5RQIa6ZKW36qNQLvLxy/BscC7B/mdLptoDs/ot9NimUXZcgCR1a2Q3o7Wi6jIgcgJcyV10Nba81ol4RdN4qPHnVZIzuo+dBkqwG3CMtB4Rj84+Qi+7zyU01hIPreoxQDXaayiGPBUUIiAlW9gsiuRWJzNnu3cvuWDLVfQIkjh7Wug58z+v2NOJ7IMdyERillhzDcvVHaq14+U= [email protected]" | ||
expectedKey, _, _, _, _ := ssh.ParseAuthorizedKey([]byte(publicKey)) | ||
|
||
pub, err := NewPublicKey(strings.NewReader(publicKey)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
identities, err := pub.Identities() | ||
if err != nil { | ||
t.Fatalf("unexpected error getting identities: %v", err) | ||
if !reflect.DeepEqual(pub.EmailAddresses(), []string{"[email protected]"}) { | ||
t.Fatalf("expected email address, got %v", pub.EmailAddresses()) | ||
} | ||
if !reflect.DeepEqual(pub.Subjects(), []string{"[email protected]"}) { | ||
t.Fatalf("expected email address as subject, got %v", pub.Subjects()) | ||
} | ||
|
||
expectedIDs := []string{"[email protected]", | ||
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDXofkiahE7uavjWvxnwkUF27qMgz7pdTwzSv0XzVG6EtirOv3PDWct4YKoXE9c0EqbxnIfYEKwEextdvB7zkgwczdJSHxf/18jQumLn/FuoCmugVSk1H5Qli/qzwBpaTnOk3WuakGuoYUl8ZAokKKgOKLA0aZJ1WRQ2ZCZggA3EkwNZiY17y9Q6HqdgQcH6XN8aAMADNVJdMAJb33hSRJjjsAPTmzBTishP8lYDoGRSsSE7/8XRBCEV5E4I8mI9GElcZwV/1KJx98mpH8QvMzXM1idFcwPRtt1NTAOshwgUU0Fu1x8lU5RQIa6ZKW36qNQLvLxy/BscC7B/mdLptoDs/ot9NimUXZcgCR1a2Q3o7Wi6jIgcgJcyV10Nba81ol4RdN4qPHnVZIzuo+dBkqwG3CMtB4Rj84+Qi+7zyU01hIPreoxQDXaayiGPBUUIiAlW9gsiuRWJzNnu3cvuWDLVfQIkjh7Wug58z+v2NOJ7IMdyERillhzDcvVHaq14+U="} | ||
|
||
sort.Strings(identities) | ||
sort.Strings(expectedIDs) | ||
if !reflect.DeepEqual(identities, expectedIDs) { | ||
t.Errorf("err getting identities from keys, got %v, expected %v", identities, expectedIDs) | ||
keyVal := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDXofkiahE7uavjWvxnwkUF27qMgz7pdTwzSv0XzVG6EtirOv3PDWct4YKoXE9c0EqbxnIfYEKwEextdvB7zkgwczdJSHxf/18jQumLn/FuoCmugVSk1H5Qli/qzwBpaTnOk3WuakGuoYUl8ZAokKKgOKLA0aZJ1WRQ2ZCZggA3EkwNZiY17y9Q6HqdgQcH6XN8aAMADNVJdMAJb33hSRJjjsAPTmzBTishP8lYDoGRSsSE7/8XRBCEV5E4I8mI9GElcZwV/1KJx98mpH8QvMzXM1idFcwPRtt1NTAOshwgUU0Fu1x8lU5RQIa6ZKW36qNQLvLxy/BscC7B/mdLptoDs/ot9NimUXZcgCR1a2Q3o7Wi6jIgcgJcyV10Nba81ol4RdN4qPHnVZIzuo+dBkqwG3CMtB4Rj84+Qi+7zyU01hIPreoxQDXaayiGPBUUIiAlW9gsiuRWJzNnu3cvuWDLVfQIkjh7Wug58z+v2NOJ7IMdyERillhzDcvVHaq14+U=" | ||
expectedID := identity.Identity{Crypto: expectedKey, Raw: []byte(keyVal)} | ||
ids, err := pub.Identities() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(ids) != 1 { | ||
t.Errorf("too many identities, expected 1, got %v", len(ids)) | ||
} | ||
if !reflect.DeepEqual(ids[0].Crypto.(ssh.PublicKey).Marshal(), expectedID.Crypto.(ssh.PublicKey).Marshal()) { | ||
t.Errorf("certificates did not match") | ||
} | ||
if !reflect.DeepEqual(ids[0].Raw, expectedID.Raw) { | ||
t.Errorf("raw identities did not match, expected %v, got %v", string(ids[0].Raw), string(expectedID.Raw)) | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.