Skip to content

Commit

Permalink
Pay closer attention to namespaces when working with secrets.
Browse files Browse the repository at this point in the history
Fixes #1467.
  • Loading branch information
Flynn committed Apr 25, 2019
1 parent 146ca59 commit 6209022
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
17 changes: 16 additions & 1 deletion ambassador/ambassador/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class Config:
'kubernetesendpointresolver': "resolvers",
'kubernetesserviceresolver': "resolvers",
'ratelimitservice': "ratelimit_configs",
'secret': "secret",
'tcpmapping': "tcpmappings",
'tlscontext': "tls_contexts",
'tracingservice': "tracing_configs",
Expand Down Expand Up @@ -509,6 +508,22 @@ def handle_module(self, resource: ACResource) -> None:

self.safe_store("modules", module_resource)

def handle_secret(self, resource: ACResource) -> None:
"""
Handles a Secret resource. We need a handler for this because the key needs to be
the name plus the namespace, not the name.
"""

storage = self.config.setdefault('secrets', {})
key = resource.rkey

if key in storage:
self.post_error("%s defines %s %s, which is already defined by %s" %
(resource, resource.kind, key, storage[key].location),
resource=resource)

storage[key] = resource

def handle_service(self, resource: ACResource) -> None:
"""
Handles a Service resource. We need a handler for this because the key needs to be
Expand Down
10 changes: 7 additions & 3 deletions ambassador/ambassador/ir/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,16 @@ def save_resource(self, resource: IRResource) -> IRResource:

# Save secrets from our aconf.
def save_secret_info(self, aconf):
aconf_secrets = aconf.get_config("secret") or {}
aconf_secrets = aconf.get_config("secrets") or {}

for secret_name, aconf_secret in aconf_secrets.items():
for secret_key, aconf_secret in aconf_secrets.items():
# Ignore anything that doesn't at least have a public half.
if aconf_secret.get('tls_crt'):
secret_info = SecretInfo.from_aconf_secret(aconf_secret)
secret_name = secret_info.name
secret_namespace = secret_info.namespace

self.logger.debug(f'saving {secret_name}.{secret_namespace} (from {secret_key}) in secret_info')
self.secret_info[f'{secret_name}.{secret_namespace}'] = secret_info

# Save TLS contexts from the aconf into the IR. Note that the contexts in the aconf
Expand Down Expand Up @@ -308,7 +310,9 @@ def resolve_secret(self, context: IRTLSContext, secret_name: str) -> SavedSecret
# OK, do we have a secret_info for it??
secret_info = self.secret_info.get(ss_key, None)

if not secret_info:
if secret_info:
self.logger.debug(f"resolve_secret {ss_key}: found secret_info")
else:
# No secret_info, so ask the secret_handler to find us one.
self.logger.debug(f"resolve_secret {ss_key}: asking handler to load")
secret_info = self.secret_handler.load_secret(context, secret_name, namespace)
Expand Down
24 changes: 24 additions & 0 deletions ambassador/tests/t_redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ def requirements(self):
# only check https urls since test readiness will only end up barfing on redirect
yield from (r for r in super().requirements() if r[0] == "url" and r[1].url.startswith("https"))

def manifests(self):
return super().manifests() + """
---
apiVersion: v1
kind: Secret
metadata:
name: redirect-cert
namespace: plain-namespace
type: kubernetes.io/tls
data:
tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURwakNDQW82Z0F3SUJBZ0lKQUpxa1Z4Y1RtQ1FITUEwR0NTcUdTSWIzRFFFQkN3VUFNR2d4Q3pBSkJnTlYKQkFZVEFsVlRNUXN3Q1FZRFZRUUlEQUpOUVRFUE1BMEdBMVVFQnd3R1FtOXpkRzl1TVJFd0R3WURWUVFLREFoRQpZWFJoZDJseVpURVVNQklHQTFVRUN3d0xSVzVuYVc1bFpYSnBibWN4RWpBUUJnTlZCQU1NQ1d4dlkyRnNhRzl6CmREQWVGdzB4T0RFd01UQXhNREk1TURKYUZ3MHlPREV3TURjeE1ESTVNREphTUdneEN6QUpCZ05WQkFZVEFsVlQKTVFzd0NRWURWUVFJREFKTlFURVBNQTBHQTFVRUJ3d0dRbTl6ZEc5dU1SRXdEd1lEVlFRS0RBaEVZWFJoZDJseQpaVEVVTUJJR0ExVUVDd3dMUlc1bmFXNWxaWEpwYm1jeEVqQVFCZ05WQkFNTUNXeHZZMkZzYUc5emREQ0NBU0l3CkRRWUpLb1p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mNFh0aVRqUEFjWlg3RmdtT3Q5Uit3TlFQK0dFNjZoV0JpKzBWCmVLV3prV0lXeS9sTVZCSW0zVWtlSlRCT3NudTFVaGhXbm5WVDhFeWhEY1FxcndPSGlhaUo3bFZSZmRoRWFyQysKSnpaU0czOHVZUVlyc0lITnRVZFgySmdPCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K
"""

def config(self):
# Use self here, not self.target, because we want the TLS module to
# be annotated on the Ambassador itself.
Expand All @@ -27,6 +50,7 @@ def config(self):
config:
server:
enabled: True
secret: redirect-cert
redirect_cleartext_from: 8080
""")

Expand Down

0 comments on commit 6209022

Please sign in to comment.