def create_secret(token, username, user, groups=None):
    sani_name = re.sub('[^0-9a-z.-]+', '-', user.lower())
    secret_id = 'auth-{}-{}'.format(sani_name, generate_rfc1123(10))
    token_delim = '::'
    if (token_delim not in token):
        token = '{}::{}'.format(user, token)
    context = {'type': AUTH_SECRET_TYPE, 'secret_name': secret_id, 'secret_namespace': AUTH_SECRET_NS, 'user': b64encode(user.encode('UTF-8')).decode('utf-8'), 'username': b64encode(username.encode('UTF-8')).decode('utf-8'), 'password': b64encode(token.encode('UTF-8')).decode('utf-8'), 'groups': (b64encode(groups.encode('UTF-8')).decode('utf-8') if groups else '')}
    with tempfile.NamedTemporaryFile() as tmp_manifest:
        render('cdk.master.auth-webhook-secret.yaml', tmp_manifest.name, context=context)
        if kubernetes_common.kubectl_manifest('apply', tmp_manifest.name):
            hookenv.log('Created secret for {}'.format(username))
            return True
        else:
            hookenv.log('WARN: Unable to create secret for {}'.format(username))
            return False
def create_secret(token, username, user, groups=None):
    sani_name = re.sub('[^0-9a-z.-]+', '-', user.lower())
    secret_id = 'auth-{}-{}'.format(sani_name, generate_rfc1123(10))
    token_delim = '::'
    if (token_delim not in token):
        token = '{}::{}'.format(user, token)
    context = {'type': AUTH_SECRET_TYPE, 'secret_name': secret_id, 'secret_namespace': AUTH_SECRET_NS, 'user': b64encode(user.encode('UTF-8')).decode('utf-8'), 'username': b64encode(username.encode('UTF-8')).decode('utf-8'), 'password': b64encode(token.encode('UTF-8')).decode('utf-8'), 'groups': (b64encode(groups.encode('UTF-8')).decode('utf-8') if groups else )}
    with tempfile.NamedTemporaryFile() as tmp_manifest:
        render('cdk.master.auth-webhook-secret.yaml', tmp_manifest.name, context=context)
        if kubernetes_common.kubectl_manifest('apply', tmp_manifest.name):
            hookenv.log('Created secret for {}'.format(username))
            return True
        else:
            hookenv.log('WARN: Unable to create secret for {}'.format(username))
            return False