Giter VIP home page Giter VIP logo

cloud-functions-hello-world's Introduction

README

Local development

mkvirtualenv soxaas
./scripts.sh init_workspace

env_vars

# env_vars file
export GCF_REGION=
export GCP_PROJECT_ID=
export GOOGLE_CLOUD_PROJECT=$GCP_PROJECT_ID
export GCP_LOCATION=global

export GCP_KMS_KEYRING=
export GCP_KMS_KEYNAME=
export GCP_KMS_ROLE=roles/cloudkms.cryptoKeyDecrypter
export GCP_KMS_KEY_ID=projects/$GCP_PROJECT_ID/locations/$GCP_LOCATION/keyRings/$GCP_KMS_KEYRING/cryptoKeys/$GCP_KMS_KEYNAME

export GCP_SA_NAME=
export GCP_SA=$GCP_SA_NAME@$GCP_PROJECT_ID.iam.gserviceaccount.com

export GCS_BUCKET_PREFIX=
export GCS_BUCKET_NAME=$GCP_PROJECT_ID-$GCS_BUCKET_PREFIX
export GCS_BUCKET_ROLE=legacyBucketReader
export GCS_OBJECT_ROLE=legacyObjectReader

export BASE_URL=https://$GCF_REGION-$GCP_PROJECT_ID.cloudfunctions.net/

export GOOGLE_APPLICATION_CREDENTIALS=

app/.env.yaml

GCS_BUCKET_NAME:
GCP_KMS_KEY_ID:
source env_vars

Tests

TESTS REQUIRE SECRETS

Local tests

./scripts.sh tests

Integration Tests

./scripts.sh deploy
# > ./scripts.sh deploy
# Allow unauthenticated invocations of new function [hello_world]?
# (y/N)?  N
./scripts.sh system_tests
./scripts.sh destroy

API Tests

./scripts.sh api_tests

All tests

./scripts.sh all_tests

Secrets

  1. Crypto Key

  2. Cloud Shell

    • Login
    gcloud alpha cloud-shell ssh
    • Encrypt a key
    pip3 install google-cloud-kms --user
    python3
    from google.cloud import kms
    import base64
    
    kms_client = kms.KeyManagementServiceClient()
    
    resource_name = (
    "projects/<project-id>/" + \
    "locations/global/" + \
    "keyRings/<keyring>/" + \
    "cryptoKeys/<key>/" + \
    "cryptoKeyVersions/1")
    
    secret = kms_client.encrypt(resource_name, bytes("The chickens are in the hayloft.", "ascii"))
    base64.b64encode(secret.ciphertext) # Encrypted api token
  3. IAM policy

gcloud kms keys add-iam-policy-binding api_token \
    --location global \
    --keyring <keyring> \
    --role roles/cloudkms.cryptoKeyDecrypter \
    --member serviceAccount:<account>@<project-id>.iam.gserviceaccount.com

# Deploy with env vars
gcloud functions deploy hello_world --env-vars-file app/.env.yaml --runtime python37 --trigger-http --source app/
  1. Decrypt key
import os
import base64

from google.cloud import kms

def get_secret():
    kms_client = kms.KeyManagementServiceClient()
    return kms_client.decrypt(
        os.environ["SECRET_RESOURCE_NAME"],
        base64.b64decode(os.environ["SECRET_API_TOKEN"]),
    ).plaintext

Scripts

Required apis:

  • cloudkms.googleapis.com
  • cloudfunctions.googleapis.com
  • cloudresourcemanager.googleapis.com/

Required Roles:

  • Cloud Functions Developer
  • Service Account User

Required permissions for service account:

  • cloudkms.keyRings.create
  • cloudkms.cryptoKeys.create
  • cloudkms.cryptoKeyVersions.useToEncrypt
  • cloudkms.cryptoKeys.getIamPolicy
  • storage.objects.get

cloud-functions-hello-world's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.