GCP - Development

The General prereqs apply (java, maven, etc).

With those, you can can run locally via IntelliJ, using run configs (located in .idea/runConfigurations):

  • package install core builds the core JAR, on which implementations depend

  • gcp - run gmail builds and runs a local instance for GMail

Or from command line:

cd java/impl/gcp
mvn function:run -Drun.functionTarget=co.worklytics.psoxy.Route

By default, that serves the function from http://localhost:8080.

GMail Example

1.) run terraform init and terraform apply from infra/dev-personal to provision environment

Local

2.) run locally via IntelliJ run config

3.) execute the following to verify your proxy is working OK

Health check (verifies that your client can reach and invoke the proxy at all; and that is has sensible config)

curl -iX GET \
http://localhost:8080/ \
-H "X-Psoxy-Health-Check: true"
export PSOXY_USER_TO_IMPERSONATE={{--identifier of GCP user to impersonate (eg, mailbox owner, Google ID or email)--}}
curl -X GET \
http://localhost:8080/gmail/v1/users/me/messages \
-H "X-Psoxy-User-To-Impersonate: $(echo $PSOXY_USER_TO_IMPERSONATE)"

Using a message id you grab from that:

curl -X GET \
http://localhost:8080/gmail/v1/users/me/messages/1743b19234726ef3f\?format=metadata \
-H "X-Psoxy-User-To-Impersonate: $(echo $PSOXY_USER_TO_IMPERSONATE)"

Cloud

1.) deploy to GCP using Terraform (see infra/). Follow steps in any TODO files it generates.

2.) Set your env vars: (these should be in a TODO file generated by terraform in prev step

export PSOXY_GCP_PROJECT={{--GCP project id that hosts your instance--}}
export PSOXY_GCP_REGION=us-central1 # change this to whatever the default is for you project
export PSOXY_HOST=`echo $PSOXY_GCP_REGION"-"$PSOXY_GCP_PROJECT`

3.) grant yourself access (probably not needed if you have primitive role in project, like Owner or Editor)

gcloud alpha functions add-iam-policy-binding psoxy-gmail --region=$PSOXY_GCP_REGION --member=user:$(gcloud config get-value core/account) --role=roles/cloudfunctions.invoker --project=$PSOXY_PROJECT_ID

alternatively, you can add Terraform resource for this to your Terraform config, and apply it again:

resource "google_cloudfunctions_function_iam_member" "member" {
  project        = "YOUR_GCP_PROJECT_ID_HERE"
  region         = "YOUR_GCP_REGION_OF_YOUR_CLOUD_FUNCTION_HERE" # eg, us-central1
  cloud_function = "psoxy-gmail" #TODO: change if you're doing something OTHER than gmail
  role           = "roles/cloudfunctions.invoker"
  member         = "user:YOUR_EMAIL_HERE"
}

Either way, if this function is for prod use, please remove these grants after you're finished testing.

4.) invocation examples

curl -X GET \
https://`echo $PSOXY_HOST`.cloudfunctions.net/psoxy-gmail/gmail/v1/users/me/messages \
-H "Authorization: Bearer $(gcloud auth print-identity-token)" \
-H "X-Psoxy-User-To-Impersonate: $(echo $PSOXY_USER_TO_IMPERSONATE)"
curl -X GET \
https://`echo $PSOXY_HOST`.cloudfunctions.net/psoxy-gmail/gmail/v1/users/me/messages/17c3b1911726ef3f\?format=metadata \
-H "Authorization: Bearer $(gcloud auth print-identity-token)" \
-H "X-Psoxy-User-To-Impersonate: $(echo $PSOXY_USER_TO_IMPERSONATE)"
curl -X GET \
https://`echo $PSOXY_HOST`.cloudfunctions.net/psoxy-google-chat/admin/reports/v1/activity/users/all/applications/chat \
-H "Authorization: Bearer $(gcloud auth print-identity-token)" \
-H "X-Psoxy-User-To-Impersonate: $(echo $PSOXY_USER_TO_IMPERSONATE)"

Last updated