forked from cncamp/101
-
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.
- Loading branch information
Showing
31 changed files
with
1,657 additions
and
140 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,135 @@ | ||
### https://raw.githubusercontent.com/istio/istio/release-1.12/security/tools/jwt/samples/gen-jwt.py | ||
|
||
#!/usr/bin/python | ||
|
||
# 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. | ||
|
||
"""Python script generates a JWT signed with custom private key. | ||
Example: | ||
./gen-jwt.py --iss example-issuer --aud foo,bar --claims=email:[email protected],dead:beef key.pem -listclaim key1 val2 val3 -listclaim key2 val3 val4 | ||
""" | ||
from __future__ import print_function | ||
import argparse | ||
import copy | ||
import time | ||
|
||
from jwcrypto import jwt, jwk | ||
|
||
|
||
def main(args): | ||
"""Generates a signed JSON Web Token from local private key.""" | ||
with open(args.key) as f: | ||
pem_data = f.read() | ||
f.closed | ||
|
||
pem_data_encode = pem_data.encode("utf-8") | ||
key = jwk.JWK.from_pem(pem_data_encode) | ||
|
||
if args.jwks: | ||
with open(args.jwks, "w+") as fout: | ||
fout.write("{ \"keys\":[ ") | ||
fout.write(key.export(private_key=False)) | ||
fout.write("]}") | ||
fout.close | ||
|
||
now = int(time.time()) | ||
payload = { | ||
# expire in one hour. | ||
"exp": now + args.expire, | ||
"iat": now, | ||
} | ||
if args.iss: | ||
payload["iss"] = args.iss | ||
if args.sub: | ||
payload["sub"] = args.sub | ||
else: | ||
payload["sub"] = args.iss | ||
|
||
if args.aud: | ||
if "," in args.aud: | ||
payload["aud"] = args.aud.split(",") | ||
else: | ||
payload["aud"] = args.aud | ||
|
||
if args.claims: | ||
for item in args.claims.split(","): | ||
k, v = item.split(':') | ||
payload[k] = v | ||
|
||
if args.listclaim: | ||
for item in args.listclaim: | ||
if (len(item) > 1): | ||
k = item[0] | ||
v = item[1:] | ||
payload[k] = v | ||
|
||
if args.nestedclaim: | ||
nested = {} | ||
for item in args.nestedclaim: | ||
if (len(item) > 1): | ||
k = item[0] | ||
v = item[1:] | ||
if len(v) == 1: | ||
v = v[0] | ||
nested[k] = v | ||
nested["nested-2"] = copy.copy(nested) | ||
payload["nested"] = nested | ||
|
||
token = jwt.JWT(header={"alg": "RS256", "typ": "JWT", "kid": key.key_id}, | ||
claims=payload) | ||
|
||
token.make_signed_token(key) | ||
|
||
return token.serialize() | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
# positional arguments | ||
parser.add_argument( | ||
'key', | ||
help='The path to the key pem file. The key can be generated with openssl command: `openssl genrsa -out key.pem 2048`') | ||
# optional arguments | ||
parser.add_argument("-iss", "--iss", | ||
default="[email protected]", | ||
help="iss claim. Default is `[email protected]`") | ||
parser.add_argument("-aud", "--aud", | ||
help="aud claim. This is comma-separated-list of audiences") | ||
parser.add_argument("-sub", "--sub", | ||
help="sub claim. If not provided, it is set to the same as iss claim.") | ||
parser.add_argument("-claims", "--claims", | ||
help="Other claims in format name1:value1,name2:value2 etc. Only string values are supported.") | ||
parser.add_argument("-jwks", "--jwks", | ||
help="Path to the output file for JWKS.") | ||
parser.add_argument("-expire", "--expire", type=int, default=3600, | ||
help="JWT expiration time in second. Default is 1 hour.") | ||
parser.add_argument( | ||
"-listclaim", | ||
"--listclaim", | ||
action='append', | ||
nargs='+', | ||
help="A list claim in format key1 value2 value3... Only string values are supported. Multiple list claims can be specified, e.g., -listclaim key1 val2 val3 -listclaim key2 val3 val4.") | ||
parser.add_argument( | ||
"-nestedclaim", | ||
"--nestedclaim", | ||
action='append', | ||
nargs='+', | ||
help="Nested claim in format key value1 [value2 ...], only string values are supported, will be added under the nested key in the JWT payload. " | ||
"Multiple nested claims can be specified, e.g., -nestedclaim key1 val2 val3 -nestedclaim key2 val3 val4." | ||
) | ||
print(main(parser.parse_args())) |
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,89 @@ | ||
### setup | ||
``` | ||
kubectl create ns foo | ||
kubectl apply -f <(istioctl kube-inject -f samples/httpbin/httpbin.yaml) -n foo | ||
kubectl apply -f <(istioctl kube-inject -f samples/sleep/sleep.yaml) -n foo | ||
kubectl create ns bar | ||
kubectl apply -f <(istioctl kube-inject -f samples/httpbin/httpbin.yaml) -n bar | ||
kubectl apply -f <(istioctl kube-inject -f samples/sleep/sleep.yaml) -n bar | ||
kubectl create ns legacy | ||
kubectl apply -f samples/httpbin/httpbin.yaml -n legacy | ||
kubectl apply -f samples/sleep/sleep.yaml -n legacy | ||
``` | ||
### check sleep.bar to httpbin.foo reachability, return 200 | ||
``` | ||
kubectl exec "$(kubectl get pod -l app=sleep -n bar -o jsonpath={.items..metadata.name})" -c sleep -n bar -- curl http://httpbin.foo:8000/ip -s -o /dev/null -w "%{http_code}\n" | ||
``` | ||
### This one-liner command conveniently iterates through all reachability combinations: | ||
``` | ||
for from in "foo" "bar" "legacy"; do for to in "foo" "bar" "legacy"; do kubectl exec "$(kubectl get pod -l app=sleep -n ${from} -o jsonpath={.items..metadata.name})" -c sleep -n ${from} -- curl -s "http://httpbin.${to}:8000/ip" -s -o /dev/null -w "sleep.${from} to httpbin.${to}: %{http_code}\n"; done; done | ||
``` | ||
``` | ||
sleep.foo to httpbin.foo: 200 | ||
sleep.foo to httpbin.bar: 200 | ||
sleep.foo to httpbin.legacy: 200 | ||
sleep.bar to httpbin.foo: 200 | ||
sleep.bar to httpbin.bar: 200 | ||
sleep.bar to httpbin.legacy: 200 | ||
sleep.legacy to httpbin.foo: 200 | ||
sleep.legacy to httpbin.bar: 200 | ||
sleep.legacy to httpbin.legacy: 200 | ||
``` | ||
### check connectivity from host | ||
``` | ||
k get po -n foo -w -owide | ||
curl 192.168.166.178 | ||
``` | ||
### check peerauthentication | ||
``` | ||
kubectl get peerauthentication --all-namespaces | ||
``` | ||
### there is no dr | ||
``` | ||
kubectl get destinationrules.networking.istio.io --all-namespaces -o yaml | grep "host:" | ||
``` | ||
### display headers to check the actual sidecar to sidecar communication is through mtls | ||
``` | ||
kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl -s http://httpbin.foo:8000/headers | ||
``` | ||
### while the communication from host to sidecar is through http | ||
``` | ||
curl 192.168.166.178/headers | ||
``` | ||
### Globally enabling Istio mutual TLS in STRICT mode | ||
``` | ||
kubectl apply -f - <<EOF | ||
apiVersion: security.istio.io/v1beta1 | ||
kind: PeerAuthentication | ||
metadata: | ||
name: "default" | ||
namespace: "istio-system" | ||
spec: | ||
mtls: | ||
mode: STRICT | ||
EOF | ||
``` | ||
### check connectivity again and the connection from legacy to foo and bar are broken | ||
``` | ||
for from in "foo" "bar" "legacy"; do for to in "foo" "bar" "legacy"; do kubectl exec "$(kubectl get pod -l app=sleep -n ${from} -o jsonpath={.items..metadata.name})" -c sleep -n ${from} -- curl "http://httpbin.${to}:8000/ip" -s -o /dev/null -w "sleep.${from} to httpbin.${to}: %{http_code}\n"; done; done | ||
``` | ||
### overwrite the global rule | ||
``` | ||
cat <<EOF | kubectl apply -n foo -f - | ||
apiVersion: security.istio.io/v1beta1 | ||
kind: PeerAuthentication | ||
metadata: | ||
name: "overwrite-example" | ||
namespace: "foo" | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: httpbin | ||
mtls: | ||
mode: DISABLE | ||
EOF | ||
``` | ||
### check connectivity again and the connection from legacy to foo is back and bar is broken | ||
``` | ||
for from in "foo" "bar" "legacy"; do for to in "foo" "bar" "legacy"; do kubectl exec "$(kubectl get pod -l app=sleep -n ${from} -o jsonpath={.items..metadata.name})" -c sleep -n ${from} -- curl "http://httpbin.${to}:8000/ip" -s -o /dev/null -w "sleep.${from} to httpbin.${to}: %{http_code}\n"; done; done | ||
``` |
Oops, something went wrong.