Skip to content

Commit

Permalink
json auth working
Browse files Browse the repository at this point in the history
  • Loading branch information
GoesToEleven committed Nov 15, 2015
1 parent 6ce729e commit f09806b
Show file tree
Hide file tree
Showing 32 changed files with 666 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ builds/**/images/*
*.jpeg

# security / ssl
*.pem
*.pem
*.Xjson
2 changes: 1 addition & 1 deletion 22_go-routines/05_gomaxprocs_parallelism/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"runtime"
"sync"
"time"
"runtime"
)

var wg sync.WaitGroup
Expand Down
6 changes: 3 additions & 3 deletions 22_go-routines/06_race-condition/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"math/rand"
"sync"
"time"
"math/rand"
)

var wg sync.WaitGroup
Expand All @@ -22,7 +22,7 @@ func incrementor(s string) {
for i := 0; i < 20; i++ {
x := counter
x++
time.Sleep(time.Duration(rand.Intn(3))*time.Millisecond)
time.Sleep(time.Duration(rand.Intn(3)) * time.Millisecond)
counter = x
fmt.Println(s, i, "Counter:", counter)
}
Expand All @@ -31,4 +31,4 @@ func incrementor(s string) {

// go run -race main.go
// vs
// go run main.go
// go run main.go
6 changes: 3 additions & 3 deletions 22_go-routines/07_mutex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"math/rand"
"sync"
"time"
"math/rand"
)

var wg sync.WaitGroup
Expand All @@ -21,7 +21,7 @@ func main() {

func incrementor(s string) {
for i := 0; i < 20; i++ {
time.Sleep(time.Duration(rand.Intn(20))*time.Millisecond)
time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond)
mutex.Lock()
counter++
fmt.Println(s, i, "Counter:", counter)
Expand All @@ -32,4 +32,4 @@ func incrementor(s string) {

// go run -race main.go
// vs
// go run main.go
// go run main.go
11 changes: 4 additions & 7 deletions 22_go-routines/08_atomicity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"fmt"
"math/rand"
"sync"
"time"
"math/rand"
"sync/atomic"
"time"
)

var wg sync.WaitGroup
Expand All @@ -21,10 +21,7 @@ func main() {

func incrementor(s string) {
for i := 0; i < 20; i++ {
time.Sleep(time.Duration(rand.Intn(3))*time.Millisecond)
// race:
// counter++
// no race:
time.Sleep(time.Duration(rand.Intn(3)) * time.Millisecond)
atomic.AddInt64(&counter, 1)
fmt.Println(s, i, "Counter:", counter)
}
Expand All @@ -33,4 +30,4 @@ func incrementor(s string) {

// go run -race main.go
// vs
// go run main.go
// go run main.go
8 changes: 4 additions & 4 deletions 22_go-routines/09_channels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"time"
"math/rand"
"math/rand"
"sync/atomic"
"time"
)

var counter int
Expand All @@ -22,7 +22,7 @@ func main() {

func incrementor(s string) {
for i := 0; i < 20; i++ {
time.Sleep(time.Duration(rand.Intn(3))*time.Millisecond)
time.Sleep(time.Duration(rand.Intn(3)) * time.Millisecond)
c <- 1
fmt.Println(s, i)
if i == 19 {
Expand Down Expand Up @@ -51,4 +51,4 @@ func puller() {

// go run -race main.go
// vs
// go run main.go
// go run main.go
3 changes: 2 additions & 1 deletion 51_appengine-introduction/01_hello-world/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ func init() {

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, worlddddddddxxxxxx!")
}
w.Write([]byte("Hello, world!"))
}
55 changes: 55 additions & 0 deletions 59_appengine-GCS-storage/00_GCS-setup/00_GCS-setup.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
THIS PRESENTATION IS HELPFUL:
https://docs.google.com/presentation/d/14NA_G-Wv5aTrRYqti5HIt3oie-wXLBSkkAt3ljEO2y0/edit?usp=sharing


- activate GCS API

- create a bucket
-- the bucket must have a globally unique name, so maybe prefix with your app engine project ID

- create a service account
-- API's & AUTH
---- Credentials
------- generate service account
-------- create new client ID
-------- service account
-------- GENERATE NEW P12 KEY

This is the private key's password. It will not be shown again. You must present this password to use the private key.
notasecret

we can use this private key to connect to google cloud storage
but it's in the wrong format....

app engine expects a PEM and we have a p12
we have to convert .....

https://cloud.google.com/storage/docs/authentication#converting-the-private-key

# Convert the key from pkcs12 to pkcs1 (PEM).
$ cat /path/to/xxxx-privatekey.p12 | openssl pkcs12 -nodes -nocerts -passin pass:notasecret | openssl rsa > /path/to/secret.pem

for example:
cat ~/Downloads/downloaded_file.p12 | openssl pkcs12 -nodes -nocerts -passin pass:notasecret | openssl rsa > ~/Downloads/secret.pem


now we can look at this file
cat ~/Downloads/secret.pem

-these credentials let your local machine connect to your bucket
-appengine doesn't need these credentials
-we create/use them so that we can develop/test on our local machine / localhost

FROM HERE:
https://cloud.google.com/appengine/docs/go/googlecloudstorageclient/getstarted
FOLLOW THIS STEP:
- Make note of the Service Account Email Address for your project which will be in the form of <your_app_email_address>@developer.gserviceaccount.com.

you will need to get your email address key from the
-- API's & AUTH
---- Credentials

AT TERMINAL:
/path/to/AppEngSDK/dev_appserver.py . --appidentity_email_address <your_app_email_address>@developer.gserviceaccount.com --appidentity_private_key_path pem_file.pem

/usr/local/go_appengine/dev_appserver.py . --appidentity_email_address [email protected] --appidentity_private_key_path ~/Downloads/secret.pem
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
application: serious-water-88716
application: learning-1130
version: 1
runtime: go
api_version: go1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {

func handlePut(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
bucket := "golang-bootcamp"
bucket := "learning-1130-bucket-01"

hc := &http.Client{
Transport: &oauth2.Transport{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
application: serious-water-88716
application: learning-1130
version: 1
runtime: go
api_version: go1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "service_account",
"private_key_id": "860db08c451a6c4c0438df43fe9229a68664df82",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCgW8WO4BUk6bj/\n3wPjJUel1ni7ToDjvJpM3sLjDp3AghYxW9C60XPeBU9dQlXJNS6QCX2tLHmZl3BW\ntrseue939NGtsogPkge1MsJW/jrpTdkMgM+sV2T4qx+sPYYrNAnnEAb2dCk5FQTa\nCLOvH3JBP6plZMyFd4CiizeFtP00NKoVkf8I3n/RD4HA0py3W8weWeCl42qAuOgK\n+MwcOOpXiY9hf1L/DIPReIFNj5EbCc0JO1upaZBDG5ieG1lZyYoyx1C8kxADMkbH\nZjRhXd+72gMOhXjyT4b/29d08A0WaVjziOs66+ytTMD+2ohZ8P41DIbsbTWrlkwU\nMlAPo+K5AgMBAAECggEAUU22YLZkgg6uaazc/7HLHd4b4HvxBYZ+hTL8hLrIaO6L\n6gKvVckUgiEXGCWl8RoxvX3SZpcCIEmT/6MuRGnpbFyIYxT9v0c++TP/LLRBkkCS\nJ7C3mp3E5/97U4zuFcGQs8KAhTxoGjT5/MIkuEmtWD+7WSU+PP1WDxOe1v/o0SqE\nXN+c42c2oOpSFRedsbYE0T6WEPoL54pI8M6mXPU3iECfcJz5ruz7vd5ox5BDSLqu\nTuzzf2P1Qonhi4tm+PtJbcugu1Vc0xi2fMt9yOr42VeTCCn5Rq5X6uJTU3UzpjO0\nbnVdpdwlAkZXlpZE+7q+BUTJi49dKhDaYD9EwG/PwQKBgQDb4o5AMQmw4QwKy1u0\nyUN6Q6F2+gREe52wC80m1Ix4aWzTqf5U6qGZmh1+kbcPaH/mQjWUtk9rvY/ku5Yo\nDf/b7We4y/GGbdRTUiv2ni/sVaMvNKkYaNTuMzN/ocYov7H6xI88l1U5uk1n1QMK\nFsaVCS7EhLbPVcZegWReZnMhTQKBgQC6slJJEQ7x8fKdhBMBX/+NzMPCRlPY3y8f\ngQHdF+EL6cvutcZgJDTXNfPAhdh8FL0Q1K4s6/TpBl52TPyu19XdqOfJcNCv0wVO\n2cT2E/oSToMoySZrkIn8TJ/5eqbsyYv80JFdUWKIrhBgPolnwBIVGfh01ckCA3vD\nKQbLW28RHQKBgBn4166TXD6tRlvcC1f+Ud8WL/M6sthX9Zc+vZuxjWoHodgagzUK\nhc372zIBCitiv4C2H1gSdK0YNe/hjGnsxWT31MGKLVwgiuzQjurRYn2628yOns6I\nS2ZbW+DefeaARgiFwRl6EpIABHaYPYSqjgg6jnCd8G3rBsRQ6kcQONcdAoGBAI42\nASn5HXo8w4MqtNNqTZ606DTXH01mjF2iBkpkwx8xAw+dVTOswLdiOH9hNQsnPPFS\nIC7FubO7Dz9r21pPvG/xFkZCciRMSJHMnhSYGPfS6GOr/WEYI5OUxpLh078wszpr\noR8FPFpCxOSbHk1yaBO8yZ4trsSi5KY/XRaQWmUdAoGBAJI70uXHGJ0EDvu/HfS8\nBKKzMu9a0KH8+3BpIEq6hXGIbd2ERmM1ZAOESkjRBn56wKPdVOLgvnQagxvDxB8u\nlJWVCxGNZfO1+P3K6A3YcKAwRHq1TYzbyg6rIaNjZQin3eHf/2GxcUCjJcSLO+Ac\nk2ZkW29GR/HHKVCbsw0f0Uvz\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "102311566442744872575",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/account-2%40learning-1130.iam.gserviceaccount.com"
}
57 changes: 57 additions & 0 deletions 59_appengine-GCS-storage/02_NewWriter_JSON-auth/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package storage

import (
"io"
"net/http"

"golang.org/x/oauth2/google"

"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/cloud"
"google.golang.org/cloud/storage"
"io/ioutil"
)

const gcsBucket = "learning-1130-bucket-01"
const aeId = "learning-1130"

func init() {
http.HandleFunc("/put", handlePut)
}

func getCloudContext(req *http.Request) (context.Context, error) {
jsonKey, err := ioutil.ReadFile("learning-860db08c451a.Xjson")
if err != nil {
return nil, err
}

conf, err := google.JWTConfigFromJSON(
jsonKey,
storage.ScopeFullControl,
)
if err != nil {
return nil, err
}

ctx := appengine.NewContext(req)
hc := conf.Client(ctx)
return cloud.NewContext(aeId, hc), nil
}

func handlePut(res http.ResponseWriter, req *http.Request) {

cctx, err := getCloudContext(req)
if err != nil {
http.Error(res, "ERROR GETTING CCTX: "+err.Error(), 500)
return
}

writer := storage.NewWriter(cctx, gcsBucket, "exampleJSON2.txt")
io.WriteString(writer, "AGAIN WITH JSON AUTH")
err = writer.Close()
if err != nil {
http.Error(res, "ERROR WRITING TO BUCKET: "+err.Error(), 500)
return
}
}
8 changes: 8 additions & 0 deletions 59_appengine-GCS-storage/03_put-get-list_PEM-auth/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
application: learning-1130
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
script: _go_app
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)


const bucketName = "serious-water-88716.appspot.com"
const bucketName = "learning-1130-bucket-01"

func init() {
http.HandleFunc("/put", handlePut)
Expand Down Expand Up @@ -52,7 +52,7 @@ func handleGet(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
cctx := getCloudContext(ctx)

rdr, err := storage.NewReader(cctx, bucketName, "example.txt")
rdr, err := storage.NewReader(cctx, bucketName, "example8777.txt")
if err != nil {
http.Error(res, err.Error(), 500)
return
Expand Down
8 changes: 8 additions & 0 deletions 60_movie-website/02_image-upload-GCS/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
application: astute-curve-100822
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
script: _go_app
21 changes: 21 additions & 0 deletions 60_movie-website/02_image-upload-GCS/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package movieinfo

import "net/http"

type indexModel struct {
}

var fileServer = http.FileServer(http.Dir("public/"))

func handleIndex(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
fileServer.ServeHTTP(res, req)
return
}
model := &indexModel{}
err := tpl.ExecuteTemplate(res, "index", model)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
}
Loading

0 comments on commit f09806b

Please sign in to comment.