New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
credentials: check and expose SPIFFE ID #3626
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd4b52f
credentials: check and expose SPIFFE ID
ZhenLian 8d70904
add build constraints to not support <= go 1.9
ZhenLian 3ed9ae7
make test applicable only to >= go1.10
ZhenLian 6625171
fix review comments; move funcs to internal
ZhenLian 94e19d5
valid spiffe could only have one URI; don't return error for invalid,…
ZhenLian a988d1c
resolve jiangtao's comments
ZhenLian File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// +build go1.10 | ||
|
||
/* | ||
* | ||
* Copyright 2020 gRPC 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 credentials defines APIs for parsing SPIFFE ID. | ||
// | ||
// All APIs in this package are experimental. | ||
package credentials | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net/url" | ||
|
||
"google.golang.org/grpc/grpclog" | ||
) | ||
|
||
// SPIFFEIDFromState parses the SPIFFE ID from State. An error is returned only | ||
// when we are sure SPIFFE ID is used but the format is wrong. | ||
func SPIFFEIDFromState(state tls.ConnectionState) (*url.URL, error) { | ||
if len(state.PeerCertificates) == 0 || len(state.PeerCertificates[0].URIs) == 0 { | ||
return nil, nil | ||
} | ||
spiffeIDCnt := 0 | ||
ZhenLian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var spiffeID url.URL | ||
for _, uri := range state.PeerCertificates[0].URIs { | ||
if uri == nil || uri.Scheme != "spiffe" || uri.Opaque != "" || (uri.User != nil && uri.User.Username() != "") { | ||
continue | ||
} | ||
// From this point, we assume the uri is intended for a SPIFFE ID. | ||
if len(uri.Host)+len(uri.Scheme)+len(uri.RawPath)+4 > 2048 || | ||
ZhenLian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
len(uri.Host)+len(uri.Scheme)+len(uri.Path)+4 > 2048 { | ||
return nil, fmt.Errorf("invalid SPIFFE ID: total ID length larger than 2048 bytes") | ||
} | ||
if len(uri.Host) == 0 || len(uri.RawPath) == 0 || len(uri.Path) == 0 { | ||
return nil, fmt.Errorf("invalid SPIFFE ID: domain or workload ID is empty") | ||
} | ||
if len(uri.Host) > 255 { | ||
return nil, fmt.Errorf("invalid SPIFFE ID: domain length larger than 255 characters") | ||
} | ||
// We use a default deep copy since we know the User field of a SPIFFE ID | ||
// is empty. | ||
spiffeID = *uri | ||
spiffeIDCnt++ | ||
} | ||
if spiffeIDCnt == 1 { | ||
return &spiffeID, nil | ||
} else if spiffeIDCnt > 1 { | ||
// A standard SPIFFE ID should be unique. If there are more than one ID, we | ||
// should log this error but shouldn't halt the application. | ||
grpclog.Warning("invalid SPIFFE ID: multiple SPIFFE IDs") | ||
return nil, nil | ||
} | ||
// SPIFFE ID is not used. | ||
return nil, nil | ||
} |
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,158 @@ | ||
// +build go1.10 | ||
|
||
/* | ||
* | ||
* Copyright 2020 gRPC 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 credentials | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"net/url" | ||
"testing" | ||
|
||
"google.golang.org/grpc/internal/grpctest" | ||
) | ||
|
||
type s struct { | ||
grpctest.Tester | ||
} | ||
|
||
func Test(t *testing.T) { | ||
grpctest.RunSubTests(t, s{}) | ||
} | ||
|
||
func (s) TestSPIFFEIDFromState(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
urls []*url.URL | ||
// If we expect SPIFFEIDFromState to return an error. | ||
expectError bool | ||
// If we expect a SPIFFE ID to be returned. | ||
expectID bool | ||
}{ | ||
{ | ||
name: "empty URIs", | ||
urls: []*url.URL{}, | ||
expectError: false, | ||
expectID: false, | ||
}, | ||
{ | ||
name: "good SPIFFE ID", | ||
urls: []*url.URL{ | ||
{ | ||
Scheme: "spiffe", | ||
Host: "foo.bar.com", | ||
Path: "workload/wl1", | ||
RawPath: "workload/wl1", | ||
}, | ||
{ | ||
Scheme: "https", | ||
Host: "foo.bar.com", | ||
Path: "workload/wl1", | ||
RawPath: "workload/wl1", | ||
}, | ||
}, | ||
expectError: false, | ||
expectID: true, | ||
}, | ||
{ | ||
name: "invalid host", | ||
urls: []*url.URL{ | ||
{ | ||
Scheme: "spiffe", | ||
Host: "", | ||
Path: "workload/wl1", | ||
RawPath: "workload/wl1", | ||
}, | ||
}, | ||
expectError: true, | ||
expectID: false, | ||
}, | ||
{ | ||
name: "invalid path", | ||
urls: []*url.URL{ | ||
{ | ||
Scheme: "spiffe", | ||
Host: "foo.bar.com", | ||
Path: "", | ||
RawPath: "", | ||
}, | ||
}, | ||
expectError: true, | ||
expectID: false, | ||
}, | ||
{ | ||
name: "large path", | ||
urls: []*url.URL{ | ||
{ | ||
Scheme: "spiffe", | ||
Host: "foo.bar.com", | ||
Path: string(make([]byte, 2050)), | ||
RawPath: string(make([]byte, 2050)), | ||
}, | ||
}, | ||
expectError: true, | ||
expectID: false, | ||
}, | ||
{ | ||
name: "large host", | ||
urls: []*url.URL{ | ||
{ | ||
Scheme: "spiffe", | ||
Host: string(make([]byte, 256)), | ||
Path: "workload/wl1", | ||
RawPath: "workload/wl1", | ||
}, | ||
}, | ||
expectError: true, | ||
expectID: false, | ||
}, | ||
{ | ||
name: "multiple SPIFFE IDs", | ||
urls: []*url.URL{ | ||
{ | ||
Scheme: "spiffe", | ||
Host: "foo.bar.com", | ||
Path: "workload/wl1", | ||
RawPath: "workload/wl1", | ||
}, | ||
{ | ||
Scheme: "spiffe", | ||
Host: "bar.baz.com", | ||
Path: "workload/wl2", | ||
RawPath: "workload/wl2", | ||
}, | ||
}, | ||
expectError: false, | ||
expectID: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
state := tls.ConnectionState{PeerCertificates: []*x509.Certificate{{URIs: tt.urls}}} | ||
id, err := SPIFFEIDFromState(state) | ||
if got, want := err != nil, tt.expectError; got != want { | ||
t.Errorf("want expectError = %v, but got expectError = %v, with error %v", want, got, err) | ||
} | ||
if got, want := id != nil, tt.expectID; got != want { | ||
t.Errorf("want expectID = %v, but SPIFFE ID is %v", want, id) | ||
} | ||
}) | ||
} | ||
} |
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,31 @@ | ||
// +build !go1.10 | ||
|
||
/* | ||
* | ||
* Copyright 2020 gRPC 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 credentials | ||
|
||
import ( | ||
"crypto/tls" | ||
"net/url" | ||
) | ||
|
||
//TODO(ZhenLian): delete this file when we remove Go 1.9 tests. | ||
func SPIFFEIDFromState(state tls.ConnectionState) (*url.URL, error) { | ||
return nil, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't consider SPIFFE to be "common" to all transport security implementations. I don't think it makes as much sense here, at least not if the only reason to move it was because this struct is already marked as experimental.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. I put it back to TLSInfo. Since TLSInfo itself is not EXPERIMENTAL, I marked only the SPIFFEID EXPERIMENTAL. Please let me know if it works:)