-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: denis-tingajkin <denis.tingajkin@xored.com>
- Loading branch information
1 parent
d11653e
commit aa97513
Showing
17 changed files
with
349 additions
and
223 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
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 was deleted.
Oops, something went wrong.
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,85 @@ | ||
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 opa_test | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"testing" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/peer" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/tools/opa" | ||
) | ||
|
||
func Test_CurrentTokenShouldBeSigned_Server(t *testing.T) { | ||
ca, err := generateCA() | ||
require.Nil(t, err) | ||
|
||
cert, err := generateKeyPair(spiffeID, "test.com", &ca) | ||
require.Nil(t, err) | ||
|
||
token, err := jwt.New(jwt.SigningMethodES256).SignedString(cert.PrivateKey) | ||
require.Nil(t, err) | ||
|
||
validX509crt, err := x509.ParseCertificate(cert.Certificate[0]) | ||
require.Nil(t, err) | ||
|
||
p := opa.WithCurrentTokenSignedPolicy() | ||
|
||
samples := []*networkservice.Path{ | ||
{ | ||
PathSegments: []*networkservice.PathSegment{ | ||
{ | ||
Token: token, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
peerAuth := &peer.Peer{ | ||
AuthInfo: &credentials.TLSInfo{ | ||
State: tls.ConnectionState{ | ||
PeerCertificates: []*x509.Certificate{ | ||
validX509crt, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
ctx := peer.NewContext(context.Background(), peerAuth) | ||
|
||
for i, input := range samples { | ||
peerAuth.AuthInfo.(*credentials.TLSInfo).State.PeerCertificates[0] = validX509crt | ||
|
||
err = p.Check(ctx, input) | ||
require.NoError(t, err, i) | ||
|
||
invalidX509crt, err := x509.ParseCertificate(ca.Certificate[0]) | ||
require.NoError(t, err) | ||
|
||
peerAuth.AuthInfo.(*credentials.TLSInfo).State.PeerCertificates[0] = invalidX509crt | ||
|
||
err = p.Check(ctx, input) | ||
require.Error(t, err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,79 @@ | ||
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 opa | ||
|
||
import _ "embed" | ||
|
||
//go:embed policies/tokens_valid.opa | ||
var tokensValidPolicySource string | ||
|
||
//go:embed policies/prev_token_signed.opa | ||
var prevTokenSignedPolicySource string | ||
|
||
//go:embed policies/curr_token_signed.opa | ||
var currTokenSignedPolicySource string | ||
|
||
//go:embed policies/tokens_chained.opa | ||
var tokensChainedPolicySource string | ||
|
||
//go:embed policies/tokens_expired.opa | ||
var tokensExpiredPolicySource string | ||
|
||
// WithTokensValidPolicy returns default policy for checking that all tokens in the path can be decoded. | ||
func WithTokensValidPolicy() *AuthorizationPolicy { | ||
return &AuthorizationPolicy{ | ||
policySource: tokensValidPolicySource, | ||
query: "tokens_valid", | ||
checker: True("tokens_valid"), | ||
} | ||
} | ||
|
||
// WithCurrentTokenSignedPolicy returns default policy for checking that last token in path is signed. | ||
func WithCurrentTokenSignedPolicy() *AuthorizationPolicy { | ||
return &AuthorizationPolicy{ | ||
policySource: currTokenSignedPolicySource, | ||
query: "curr_token_signed", | ||
checker: True("curr_token_signed"), | ||
} | ||
} | ||
|
||
// WithPrevTokenSignedPolicy returns default policy for checking that last token in path is signed. | ||
func WithPrevTokenSignedPolicy() *AuthorizationPolicy { | ||
return &AuthorizationPolicy{ | ||
policySource: prevTokenSignedPolicySource, | ||
query: "prev_token_signed", | ||
checker: True("prev_token_signed"), | ||
} | ||
} | ||
|
||
// WithTokenChainPolicy returns default policy for checking tokens chain in path | ||
func WithTokenChainPolicy() *AuthorizationPolicy { | ||
return &AuthorizationPolicy{ | ||
policySource: tokensChainedPolicySource, | ||
query: "tokens_chained", | ||
checker: True("tokens_chained"), | ||
} | ||
} | ||
|
||
// WithTokensExpiredPolicy returns default policy for checking tokens expiration | ||
func WithTokensExpiredPolicy() *AuthorizationPolicy { | ||
return &AuthorizationPolicy{ | ||
policySource: tokensExpiredPolicySource, | ||
query: "tokens_expired", | ||
checker: False("tokens_expired"), | ||
} | ||
} |
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,28 @@ | ||
# Copyright (c) 2020 Cisco and/or its affiliates. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# 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 nsm | ||
|
||
default curr_token_signed = false | ||
default index = 0 | ||
|
||
index = input.index | ||
|
||
curr_token_signed { | ||
token := input.path_segments[index].token | ||
cert := input.auth_info.certificate | ||
io.jwt.verify_es256(token, cert) = true | ||
} |
Oops, something went wrong.