Permalink
Show file tree
Hide file tree
4 changes: 2 additions & 2 deletions
4
security/pkg/nodeagent/caclient/providers/citadel/client_test.go
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add xfcc authenticator (#39405)
* add xfcc authenticator Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * go mod Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * lint Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * add license Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * fix Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * fix folder Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * add copying file Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * remove COPYING Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * address review comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * address comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * add cidr authenticator Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * lint Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * minor changes Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * add more comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * update based on review comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * fix test Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * address comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * address comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * review comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * add authentication manager Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * add test Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * fix test Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * fix test Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * address comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com> * address comments Signed-off-by: Rama Chavali <rama.rao@salesforce.com>
- Loading branch information
1 parent
dc020d6
commit 346260e
Showing
15 changed files
with
393 additions
and
53 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
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,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2020 Alec Holmes | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
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,19 @@ | ||
| Copyright (C) 2017 Alec Thomas | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| this software and associated documentation files (the "Software"), to deal in | ||
| the Software without restriction, including without limitation the rights to | ||
| use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
| of the Software, and to permit persons to whom the Software is furnished to do | ||
| so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
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
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
117 changes: 117 additions & 0 deletions
117
security/pkg/server/ca/authenticate/xfcc_authenticator.go
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,117 @@ | ||
| // Copyright Istio 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 authenticate | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/alecholmes/xfccparser" | ||
| "google.golang.org/grpc/metadata" | ||
| "google.golang.org/grpc/peer" | ||
|
|
||
| "istio.io/istio/pilot/pkg/features" | ||
| "istio.io/istio/pkg/security" | ||
| ) | ||
|
|
||
| const ( | ||
| XfccAuthenticatorType = "XfccAuthenticator" | ||
| ) | ||
|
|
||
| // XfccAuthenticator extracts identities from Xfcc header. | ||
| type XfccAuthenticator struct{} | ||
|
|
||
| var _ security.Authenticator = &XfccAuthenticator{} | ||
|
|
||
| func (xff XfccAuthenticator) AuthenticatorType() string { | ||
| return XfccAuthenticatorType | ||
| } | ||
|
|
||
| // Authenticate extracts identities from Xfcc Header. | ||
| func (xff XfccAuthenticator) Authenticate(ctx context.Context) (*security.Caller, error) { | ||
| peerInfo, _ := peer.FromContext(ctx) | ||
| // First check if client is trusted client so that we can "trust" the Xfcc Header. | ||
| if !isTrustedAddress(peerInfo.Addr.String(), features.TrustedGatewayCIDR) { | ||
| return nil, fmt.Errorf("caller from %s is not in the trusted network. XfccAuthenticator can not be used", peerInfo.Addr.String()) | ||
| } | ||
| meta, ok := metadata.FromIncomingContext(ctx) | ||
|
|
||
| if !ok || len(meta.Get(xfccparser.ForwardedClientCertHeader)) == 0 { | ||
| return nil, nil | ||
| } | ||
| xfccHeader := meta.Get(xfccparser.ForwardedClientCertHeader)[0] | ||
| return buildSecurityCaller(xfccHeader) | ||
| } | ||
|
|
||
| // AuthenticateRequest validates Xfcc Header. | ||
| func (xff XfccAuthenticator) AuthenticateRequest(req *http.Request) (*security.Caller, error) { | ||
| xfccHeader := req.Header.Get(xfccparser.ForwardedClientCertHeader) | ||
| if len(xfccHeader) == 0 { | ||
| return nil, nil | ||
| } | ||
| return buildSecurityCaller(xfccHeader) | ||
| } | ||
|
|
||
| func buildSecurityCaller(xfccHeader string) (*security.Caller, error) { | ||
| clientCerts, err := xfccparser.ParseXFCCHeader(xfccHeader) | ||
| if err != nil { | ||
| message := fmt.Sprintf("error in parsing xfcc header: %v", err) | ||
| return nil, fmt.Errorf(message) | ||
| } | ||
| if len(clientCerts) == 0 { | ||
| message := "xfcc header does not have atleast one client certs" | ||
| return nil, fmt.Errorf(message) | ||
| } | ||
| ids := []string{} | ||
| for _, cc := range clientCerts { | ||
| ids = append(ids, cc.URI) | ||
| ids = append(ids, cc.DNS...) | ||
| if cc.Subject != nil { | ||
| ids = append(ids, cc.Subject.CommonName) | ||
| } | ||
| } | ||
|
|
||
| return &security.Caller{ | ||
| AuthSource: security.AuthSourceClientCertificate, | ||
| Identities: ids, | ||
| }, nil | ||
| } | ||
|
|
||
| func isTrustedAddress(addr string, trustedCidrs []string) bool { | ||
| for _, cidr := range trustedCidrs { | ||
| if isInRange(addr, cidr) { | ||
| return true | ||
| } | ||
| } | ||
| // Always trust local host addresses. | ||
| return net.ParseIP(addr).IsLoopback() | ||
| } | ||
|
|
||
| func isInRange(addr, cidr string) bool { | ||
| if strings.Contains(cidr, "/") { | ||
| ip, ipnet, err := net.ParseCIDR(cidr) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| if ip.To4() == nil && ip.To16() == nil { | ||
| return false | ||
| } | ||
| return ipnet.Contains(net.ParseIP(addr)) | ||
| } | ||
| return false | ||
| } |
Oops, something went wrong.