Skip to content

Instantly share code, notes, and snippets.

@jmatt
Last active August 23, 2018 23:51
Show Gist options
  • Save jmatt/564bc6ce971add210fd5852b8bf75ee6 to your computer and use it in GitHub Desktop.
Save jmatt/564bc6ce971add210fd5852b8bf75ee6 to your computer and use it in GitHub Desktop.
Google Cloud KMS Example
(ns google-cloud-kms.core
(:import (com.google.api.services.cloudkms.v1.CloudKMS)
(com.google.api.services.cloudkms.v1 CloudKMS$Builder)
(com.google.api.services.cloudkms.v1.CloudKMSScopes)
(com.google.api.client.http.javanet NetHttpTransport)
(com.google.api.client.json.jackson2 JacksonFactory)
(com.google.api.client.googleapis.auth.oauth2 GoogleCredential)))
(def transport (new NetHttpTransport))
(def json-factory (new JacksonFactory))
;; Note: The user must export GOOGLE_APPLICATION_CREDENTIALS environment variable.
(defn get-credentials []
(GoogleCredential/getApplicationDefault transport json-factory))
(defn get-scoped-credentials []
(.createScoped (get-credentials)
(com.google.api.services.cloudkms.v1.CloudKMSScopes/all)))
(def kms
(.build (.setApplicationName
(new CloudKMS$Builder
transport
json-factory
(get-scoped-credentials))
"Google Cloud KMS Example")))
(defn expanded-path [project location]
(format "projects/%s/locations/%s" project location))
(defn list-keyrings [project location]
(.execute (.list (-> kms .projects .locations .keyRings)
(expanded-path project location))))
;; Example call: (kms-core/list-keyrings "YOUR-PROJECT" "YOUR-REGION")
(defproject google-cloud-kms "0.1.1-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[com.google.apis/google-api-services-cloudkms "v1-rev26-1.23.0"]])
leia:google-cloud-kms jmatt$
⇝ lein repl
nREPL server started on port 61656 on host 127.0.0.1 - nrepl://127.0.0.1:61656
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_181-b13
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> (require '[google-cloud-kms.core :as kms-core])
nil
user=> (kms-core/list-keyrings "PROJECT" "REGION")
{"keyRings" [{"createTime" "2018-08-21T22:05:44.502897593Z", "name" "projects/PROJECT/locations/REGION/keyRings/KEY_RING_NAME_1"} {"createTime" "2018-08-23T21:00:24.058041347Z", "name" "projects/PROJECT/locations/REGION/keyRings/KEY_RING_NAME_2"}], "totalSize" 2}
user=> Bye for now!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment