forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use authentication policy to service-to-service mTLS. (istio#4089)
* Use authentication policy to service-to-service mTLS. * Lint * Remove pre-mature changes. authn policy doen't need to be part of buildListenerOpts yet. * Remove unused definitions. * Add e2e test case.
- Loading branch information
Showing
13 changed files
with
391 additions
and
24 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,93 @@ | ||
// Copyright 2018 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. | ||
|
||
// AuthN filter configuration | ||
|
||
package v1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
authn "istio.io/api/authentication/v1alpha1" | ||
meshconfig "istio.io/api/mesh/v1alpha1" | ||
"istio.io/istio/pilot/pkg/model" | ||
"istio.io/istio/pkg/log" | ||
) | ||
|
||
// getConsolidateAuthenticationPolicy returns the authentication policy for | ||
// service specified by hostname and port, if defined. | ||
// If not, it generates and output a policy that is equivalent to the legacy flag | ||
// and/or service annotation. Once these legacy flags/config deprecated, | ||
// this function can be placed by a call to store.AuthenticationPolicyByDestination | ||
// directly. | ||
func getConsolidateAuthenticationPolicy(mesh *meshconfig.MeshConfig, store model.IstioConfigStore, hostname string, port *model.Port) *authn.Policy { | ||
config := store.AuthenticationPolicyByDestination(hostname, port) | ||
if config == nil { | ||
legacyPolicy := consolidateAuthPolicy(mesh, port.AuthenticationPolicy) | ||
log.Debugf("No authentication policy found for %s:%d. Fallback to legacy authentication mode %v\n", | ||
hostname, port.Port, legacyPolicy) | ||
return legacyAuthenticationPolicyToPolicy(legacyPolicy) | ||
} | ||
|
||
return config.Spec.(*authn.Policy) | ||
} | ||
|
||
// consolidateAuthPolicy returns service auth policy, if it's not INHERIT. Else, | ||
// returns mesh policy. | ||
func consolidateAuthPolicy(mesh *meshconfig.MeshConfig, | ||
serviceAuthPolicy meshconfig.AuthenticationPolicy) meshconfig.AuthenticationPolicy { | ||
if serviceAuthPolicy != meshconfig.AuthenticationPolicy_INHERIT { | ||
return serviceAuthPolicy | ||
} | ||
// TODO: use AuthenticationPolicy for mesh policy and remove this conversion | ||
switch mesh.AuthPolicy { | ||
case meshconfig.MeshConfig_MUTUAL_TLS: | ||
return meshconfig.AuthenticationPolicy_MUTUAL_TLS | ||
case meshconfig.MeshConfig_NONE: | ||
return meshconfig.AuthenticationPolicy_NONE | ||
default: | ||
// Never get here, there are no other enum value for mesh.AuthPolicy. | ||
panic(fmt.Sprintf("Unknown mesh auth policy: %v\n", mesh.AuthPolicy)) | ||
} | ||
} | ||
|
||
// If input legacy is AuthenticationPolicy_MUTUAL_TLS, return a authentication policy equivalent | ||
// to it. Else, returns nil (implies no authentication is used) | ||
func legacyAuthenticationPolicyToPolicy(legacy meshconfig.AuthenticationPolicy) *authn.Policy { | ||
if legacy == meshconfig.AuthenticationPolicy_MUTUAL_TLS { | ||
return &authn.Policy{ | ||
Peers: []*authn.PeerAuthenticationMethod{{ | ||
Params: &authn.PeerAuthenticationMethod_Mtls{}}}, | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// requireTLS returns true if the policy use mTLS for (peer) authentication. | ||
func requireTLS(policy *authn.Policy) bool { | ||
if policy == nil { | ||
return false | ||
} | ||
if len(policy.Peers) > 0 { | ||
for _, method := range policy.Peers { | ||
switch method.GetParams().(type) { | ||
case *authn.PeerAuthenticationMethod_Mtls: | ||
return true | ||
default: | ||
continue | ||
} | ||
} | ||
} | ||
return false | ||
} |
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,86 @@ | ||
// Copyright 2018 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 v1 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
authn "istio.io/api/authentication/v1alpha1" | ||
meshconfig "istio.io/api/mesh/v1alpha1" | ||
) | ||
|
||
func TestRequireTls(t *testing.T) { | ||
cases := []struct { | ||
in authn.Policy | ||
expected bool | ||
}{ | ||
{ | ||
in: authn.Policy{}, | ||
expected: false, | ||
}, | ||
{ | ||
in: authn.Policy{ | ||
Peers: []*authn.PeerAuthenticationMethod{{ | ||
Params: &authn.PeerAuthenticationMethod_Mtls{}, | ||
}}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
in: authn.Policy{ | ||
Peers: []*authn.PeerAuthenticationMethod{{ | ||
Params: &authn.PeerAuthenticationMethod_Jwt{}, | ||
}, | ||
{ | ||
Params: &authn.PeerAuthenticationMethod_Mtls{}, | ||
}, | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
} | ||
for _, c := range cases { | ||
if got := requireTLS(&c.in); got != c.expected { | ||
t.Errorf("requireTLS(%v): got(%v) != want(%v)\n", c.in, got, c.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestLegacyAuthenticationPolicyToPolicy(t *testing.T) { | ||
cases := []struct { | ||
in meshconfig.AuthenticationPolicy | ||
expected *authn.Policy | ||
}{ | ||
{ | ||
in: meshconfig.AuthenticationPolicy_MUTUAL_TLS, | ||
expected: &authn.Policy{ | ||
Peers: []*authn.PeerAuthenticationMethod{{ | ||
Params: &authn.PeerAuthenticationMethod_Mtls{}, | ||
}}, | ||
}, | ||
}, | ||
{ | ||
in: meshconfig.AuthenticationPolicy_NONE, | ||
expected: nil, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
if got := legacyAuthenticationPolicyToPolicy(c.in); !reflect.DeepEqual(got, c.expected) { | ||
t.Errorf("legacyAuthenticationPolicyToPolicy(%v): got(%#v) != want(%#v)\n", c.in, got, c.expected) | ||
} | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
pilot/pkg/proxy/envoy/v1/testdata/authn-hello-mtls-off.yaml.golden
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,6 @@ | ||
destinations: | ||
- name: hello | ||
port: | ||
name: "http" | ||
peers: | ||
- none: null |
2 changes: 2 additions & 0 deletions
2
pilot/pkg/proxy/envoy/v1/testdata/authn-namespace-mtls-off.yaml.golden
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,2 @@ | ||
peers: | ||
- none: null |
2 changes: 2 additions & 0 deletions
2
pilot/pkg/proxy/envoy/v1/testdata/authn-namespace-mtls-on.yaml.golden
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,2 @@ | ||
peers: | ||
- mtls: null |
6 changes: 6 additions & 0 deletions
6
pilot/pkg/proxy/envoy/v1/testdata/authn-world-mtls-off.yaml.golden
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,6 @@ | ||
destinations: | ||
- name: world | ||
port: | ||
name: "http" | ||
peers: | ||
- none: null |
Oops, something went wrong.