| 1 |
// |
| 2 |
// Copyright 2021 The Sigstore Authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
package aws |
| 17 |
|
| 18 |
import "testing" |
| 19 |
|
| 20 |
func TestParseReference(t *testing.T) { |
| 21 |
tests := []struct { |
| 22 |
in string |
| 23 |
wantEndpoint string |
| 24 |
wantKeyID string |
| 25 |
wantAlias string |
| 26 |
wantErr bool |
| 27 |
}{ |
| 28 |
{ |
| 29 |
in: "awskms:///1234abcd-12ab-34cd-56ef-1234567890ab", |
| 30 |
wantEndpoint: "", |
| 31 |
wantKeyID: "1234abcd-12ab-34cd-56ef-1234567890ab", |
| 32 |
wantAlias: "", |
| 33 |
wantErr: false, |
| 34 |
}, |
| 35 |
{ |
| 36 |
// multi-region key |
| 37 |
in: "awskms:///mrk-1234abcd12ab34cd56ef1234567890ab", |
| 38 |
wantEndpoint: "", |
| 39 |
wantKeyID: "mrk-1234abcd12ab34cd56ef1234567890ab", |
| 40 |
wantAlias: "", |
| 41 |
wantErr: false, |
| 42 |
}, |
| 43 |
{ |
| 44 |
in: "awskms:///1234ABCD-12AB-34CD-56EF-1234567890AB", |
| 45 |
wantEndpoint: "", |
| 46 |
wantKeyID: "1234ABCD-12AB-34CD-56EF-1234567890AB", |
| 47 |
wantAlias: "", |
| 48 |
wantErr: false, |
| 49 |
}, |
| 50 |
{ |
| 51 |
in: "awskms://localhost:4566/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 52 |
wantEndpoint: "localhost:4566", |
| 53 |
wantKeyID: "1234abcd-12ab-34cd-56ef-1234567890ab", |
| 54 |
wantAlias: "", |
| 55 |
wantErr: false, |
| 56 |
}, |
| 57 |
{ |
| 58 |
in: "awskms:///arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 59 |
wantEndpoint: "", |
| 60 |
wantKeyID: "arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 61 |
wantAlias: "", |
| 62 |
wantErr: false, |
| 63 |
}, |
| 64 |
{ |
| 65 |
in: "awskms://localhost:4566/arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 66 |
wantEndpoint: "localhost:4566", |
| 67 |
wantKeyID: "arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 68 |
wantAlias: "", |
| 69 |
wantErr: false, |
| 70 |
}, |
| 71 |
{ |
| 72 |
in: "awskms:///alias/ExampleAlias", |
| 73 |
wantEndpoint: "", |
| 74 |
wantKeyID: "alias/ExampleAlias", |
| 75 |
wantAlias: "alias/ExampleAlias", |
| 76 |
wantErr: false, |
| 77 |
}, |
| 78 |
{ |
| 79 |
in: "awskms://localhost:4566/alias/ExampleAlias", |
| 80 |
wantEndpoint: "localhost:4566", |
| 81 |
wantKeyID: "alias/ExampleAlias", |
| 82 |
wantAlias: "alias/ExampleAlias", |
| 83 |
wantErr: false, |
| 84 |
}, |
| 85 |
{ |
| 86 |
in: "awskms:///arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias", |
| 87 |
wantEndpoint: "", |
| 88 |
wantKeyID: "arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias", |
| 89 |
wantAlias: "alias/ExampleAlias", |
| 90 |
wantErr: false, |
| 91 |
}, |
| 92 |
{ |
| 93 |
in: "awskms:///arn:aws-us-gov:kms:us-gov-west-1:111122223333:alias/ExampleAlias", |
| 94 |
wantEndpoint: "", |
| 95 |
wantKeyID: "arn:aws-us-gov:kms:us-gov-west-1:111122223333:alias/ExampleAlias", |
| 96 |
wantAlias: "alias/ExampleAlias", |
| 97 |
wantErr: false, |
| 98 |
}, |
| 99 |
{ |
| 100 |
in: "awskms:///arn:aws-us-gov:kms:us-gov-west-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 101 |
wantEndpoint: "", |
| 102 |
wantKeyID: "arn:aws-us-gov:kms:us-gov-west-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 103 |
wantAlias: "", |
| 104 |
wantErr: false, |
| 105 |
}, |
| 106 |
{ |
| 107 |
in: "awskms://localhost:4566/arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias", |
| 108 |
wantEndpoint: "localhost:4566", |
| 109 |
wantKeyID: "arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias", |
| 110 |
wantAlias: "alias/ExampleAlias", |
| 111 |
wantErr: false, |
| 112 |
}, |
| 113 |
{ |
| 114 |
// missing alias/ prefix |
| 115 |
in: "awskms:///missingalias", |
| 116 |
wantEndpoint: "", |
| 117 |
wantKeyID: "", |
| 118 |
wantAlias: "", |
| 119 |
wantErr: true, |
| 120 |
}, |
| 121 |
{ |
| 122 |
// invalid UUID |
| 123 |
in: "awskms:///1234abcd-12ab-YYYY-56ef-1234567890ab", |
| 124 |
wantEndpoint: "", |
| 125 |
wantKeyID: "", |
| 126 |
wantAlias: "", |
| 127 |
wantErr: true, |
| 128 |
}, |
| 129 |
{ |
| 130 |
// Currently, references without endpoints must use 3 |
| 131 |
// slashes. It would be nice to support this format, |
| 132 |
// but that would be harder to parse. |
| 133 |
in: "awskms://1234abcd-12ab-34cd-56ef-1234567890ab", |
| 134 |
wantEndpoint: "", |
| 135 |
wantKeyID: "", |
| 136 |
wantAlias: "", |
| 137 |
wantErr: true, |
| 138 |
}, |
| 139 |
} |
| 140 |
for _, tt := range tests { |
| 141 |
t.Run(tt.in, func(t *testing.T) { |
| 142 |
gotEndpoint, gotKeyID, gotAlias, err := ParseReference(tt.in) |
| 143 |
if (err != nil) != tt.wantErr { |
| 144 |
t.Errorf("ParseReference() error = %v, wantErr %v", err, tt.wantErr) |
| 145 |
return |
| 146 |
} |
| 147 |
if gotEndpoint != tt.wantEndpoint { |
| 148 |
t.Errorf("ParseReference() gotEndpoint = %v, want %v", gotEndpoint, tt.wantEndpoint) |
| 149 |
} |
| 150 |
if gotKeyID != tt.wantKeyID { |
| 151 |
t.Errorf("ParseReference() gotKeyID = %v, want %v", gotKeyID, tt.wantKeyID) |
| 152 |
} |
| 153 |
if gotAlias != tt.wantAlias { |
| 154 |
t.Errorf("ParseReference() gotAlias = %v, want %v", gotAlias, tt.wantAlias) |
| 155 |
} |
| 156 |
}) |
| 157 |
} |
| 158 |
} |