onthelambda

package module
v0.0.0-...-7eb4dc8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2018 License: CC0-1.0 Imports: 9 Imported by: 0

README

onthelambda

onthelambda provides a streamlined way to authenticate your Golang AWS Lambda function to Hashicorp Vault.

Usage Example

package main

import (
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/daveadams/onthelambda"
    "log"
)

func MyLambdaHandler() {
    client, err := onthelambda.VaultClient()
    if err != nil {
        log.Fatalf("ERROR: %s", err)
    }

    resp, _ := client.Logical().Read("secret/message")
    log.Printf("The secret message is '%s'", resp.Data["value"].(string))
}

func main() {
    lambda.Start(MyLambdaHandler)
}

Setup

First, you'll need Vault up and running somewhere network-accessible to your Lambda function. That's out of scope for this README, but please see the Vault documentation for more.

Then you'll need to set up an AWS authentication provider. You may already have one configured. If so, you can use that one or you can set up a new one just for this purpose. You don't need to worry about backend credentials for this authentication method. It works without any AWS credentials needing to be loaded into Vault. Or if you do have credentials loaded they don't need to have access to the AWS account your Lambda is running in.

To establish a new AWS authentication provider, run:

$ vault auth enable -path lambda -description "IAM auth for Lambdas" aws
Success! Enabled aws auth method at: lambda/

You will also need to set the iam_server_id_header_value if you wish to use the extra layer of security (as described below):

$ vault write auth/lambda/config/client \
      iam_server_id_header_value=vault.example.com

Next, you'll need to establish whatever Vault policies your Lambda will need. See the Vault Policies documentation for details.

Now you'll need to know the ARN of your Lambda execution role. You can create it with the Lambda web console or by hand. Either way it should look something like:

arn:aws:iam::987654321098:role/service-role/MyLambdaRole

IMPORTANT: You must remove any non-essential path from the role ARN unless you have configured your AWS auth provider with IAM permissions to look up roles. In this example, service-role/ is the path segment. So the principal ARN you will be specifying to Vault in the next step will be:

arn:aws:iam::987654321098:role/MyLambdaRole

Now it's time to create the Vault authentication role. It can be named anything you wish. In this case, we'll call it my-vault-role and make it periodic since onthelambda will handle renewal automatically:

$ vault write auth/lambda/role/my-vault-role \
      auth_type=iam \
      period=14400 \
      policies=list-of,vault-policies,separated-by-commas \
      resolve_aws_unique_ids=false \
      bound_iam_principal_arn=arn:aws:iam::987654321098:role/MyLambdaRole

Now you are ready to configure your Lambda.

Configuration

All configuration is done with environment variables:

  • VAULT_ADDR (Required) The URL of the Vault instance, eg https://myvault.example.com.
  • VAULT_AUTH_PROVIDER (Required) The relative path of the AWS authentication provider, eg lambda for auth/lambda in the example above.
  • VAULT_AUTH_ROLE (Required) The name of the Vault role to authenticate to, eg my-vault-role in the example above.
  • VAULT_AUTH_HEADER (Optional, but recommended) The value of the X-Vault-AWS-IAM-Server-ID HTTP header to be included in the signed STS request this code uses to authenticate. This value is often set to the URL or DNS name of the Vault server to prevent potential replay attacks.

That should be all that is required to get up and running.

License

This software is public domain. No rights are reserved. See LICENSE for more information.

Documentation

Overview

The onthelambda package provides IAM-based Vault authentication for AWS Lambda. You must specify the VAULT_ADDR, VAULT_AUTH_PROVIDER, and VAULT_AUTH_ROLE environment variables. Then it's best practice to call VaultClient() once at the beginning of your Lambda handler function to ensure the client has a valid token that will last for the duration of the Lambda run.

Index

Constants

View Source
const VaultAuthHeaderName = "X-Vault-AWS-IAM-Server-ID"

Variables

This section is empty.

Functions

func RenewToken

func RenewToken() error

Renew the token if it is renewable. If it isn't, or if it's expired, refresh authentication instead. This is typically called internally.

func VaultAuth

func VaultAuth() error

Call VaultAuth() to authenticate the Lambda execution role to the Vault auth context specified by the VAULT_ADDR, VAULT_AUTH_PROVIDER, and VAULT_AUTH_ROLE environment variables. If no error is returned, then VaultClient is ready to go. This function is typically called internally.

This code was adapted from Hashicorp Vault:

https://github.com/hashicorp/vault/blob/e2bb2ec3b93a242a167f763684f93df867bb253d/builtin/credential/aws/cli.go#L78

func VaultClient

func VaultClient() (*vault.Client, error)

VaultClient() returns a configured and authenticated Vault client object. If the client does not yet exist, it is created and authenticated. If it does exist but the token is expired or near expiration, the token will be renewed if possible, or a new token will be acquired.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL