Skip to content

kitgen implementation #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 123 commits into from
Dec 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
123 commits
Select commit Hold shift + click to select a range
6777c8b
cmd/kitgen: parse
peterbourgon Jul 3, 2016
bea53cb
First stymie
nyarly Jul 15, 2017
d87c2ef
Merge branch 'kitgen' of github.com:nyarly/kit into kitgen
nyarly Jul 15, 2017
c302638
Sketch developing
nyarly Jul 16, 2017
2eadf3a
Broken sketch.
nyarly Jul 17, 2017
feb1308
Further progress - still doesn't build
nyarly Jul 18, 2017
cfbb55d
Need to collect ast.Exprs, not strings
nyarly Jul 18, 2017
191e38b
Running downhill.
nyarly Jul 19, 2017
961b593
sd: add Stop method to Instancer interface
peterbourgon Jul 21, 2017
b2b79e8
Fruitful avenue. Committing for travel.
nyarly Jul 24, 2017
7e30257
Needs uniquify for varnames
nyarly Jul 26, 2017
1dbe8fa
Track gauge values and support default tags for dogstatsd
Jul 30, 2017
e40ee5b
Service generation tests work
nyarly Jul 31, 2017
b04b2e2
add failing test for cloudwatch metrics, that are not reset
feliksik Jul 31, 2017
2a3c64e
Refactor cloudwatch: Reset().Walk() on every Send(), like influx impl…
feliksik Jul 31, 2017
1f238bf
Tolerate that there may not be any lables, if the teststat.FillCounte…
feliksik Jul 31, 2017
002c376
Use Cloudwatch options in the struct itself, which is cleaner
feliksik Aug 1, 2017
ab2a990
sd: fix TestDefaultEndpointer flake, hopefully
peterbourgon Aug 3, 2017
aa9583c
util/conn: more detail for flaky test
peterbourgon Aug 3, 2017
8e03b4c
Merge pull request #596 from go-kit/fix-test-flake
peterbourgon Aug 3, 2017
335e7c7
removed deprecated functions
travissalascox Aug 4, 2017
c66fcdb
Merge pull request #591 from feliksik/bug/cloudwatch-reset-counter
peterbourgon Aug 7, 2017
7c0038d
Merge pull request #598 from travissalascox/master
peterbourgon Aug 7, 2017
866d91f
Functions extracted from inline codefile
nyarly Aug 7, 2017
f583a20
Cleaner/easier way for user to specify Cloudwatch metric percentiles.
feliksik Aug 8, 2017
c961428
fix test to read quantile metrics with p prefix
feliksik Aug 9, 2017
0be9fba
test cloudwatch.WithPercentiles()
feliksik Aug 10, 2017
6d351cc
Handles anonymous fields for all parts of interface
nyarly Aug 13, 2017
cd6c724
Handles underscore param names, produces compile-able code
nyarly Aug 14, 2017
4d9f525
do not prefix metrics with 'p', just like it was previously.
feliksik Aug 14, 2017
bcc4b77
Merge pull request #588 from jfhamlin/fix/dogstatsd
peterbourgon Aug 14, 2017
ca3a8b3
Fix for dogstatsd metrics with default tags and no labelValues
Aug 15, 2017
c2c2e83
Converted flat to a layout - proceeding to implement default
nyarly Aug 15, 2017
9368400
Convenience function for formatting to a tree
nyarly Aug 15, 2017
d3c6931
Fix spelling of deregisters
elliotwms Aug 16, 2017
bd8e098
Merge pull request #603 from elliotwms/patch-1
peterbourgon Aug 16, 2017
94d041d
Merge pull request #599 from feliksik/feliksik/easy-percentiles
peterbourgon Aug 16, 2017
f01651a
Merge pull request #602 from jfhamlin/fix/dogstatsd
peterbourgon Aug 17, 2017
b48aaea
Add basic auth middleware
Aug 19, 2017
a5fca17
Default layout
nyarly Aug 20, 2017
2f72aff
Need to handle mutating trees more effectively to do default layout.
nyarly Aug 22, 2017
8c6ad4e
Basic Auth: optimize memory allocation.
Aug 28, 2017
e317ca4
Fix typo
gosharplite Aug 28, 2017
37a2080
cache required creds' slices
Aug 29, 2017
f7593d1
Merge pull request #606 from gosharplite/master
peterbourgon Aug 29, 2017
6b4ca49
Clean up comment
Aug 29, 2017
8a45441
Replacing idents successfully
nyarly Aug 31, 2017
355d3e3
improve error handling and style
Sep 2, 2017
d357b9e
Constructs import paths usefully
nyarly Sep 5, 2017
74f0d79
Some debugging - transit
nyarly Sep 11, 2017
4e222a3
Set time unit on metrics.Timer
esenac Sep 15, 2017
0b39753
Changes as per code review
esenac Sep 15, 2017
0e30e42
fix missing comma in example histogram code
Sep 16, 2017
7862b3f
Merge pull request #605 from DimaSalakhov/basicAuth
peterbourgon Sep 17, 2017
f99388b
Merge pull request #611 from daniel-garcia/fixMissingComma
peterbourgon Sep 17, 2017
b5c889e
Merge pull request #610 from esenac/timer-unit
peterbourgon Sep 17, 2017
b1eb1a0
update_deps.bash: handle detached HEAD better
peterbourgon Sep 17, 2017
f3cb2c0
.travis.yml: go1.9 + tip exclusively
peterbourgon Sep 17, 2017
5d18314
circle.yml: go1.9 exclusively
peterbourgon Sep 17, 2017
e9af46a
Merge pull request #583 from go-kit/instancer-stop
peterbourgon Sep 17, 2017
0d313fb
Merge pull request #613 from go-kit/version-changes
peterbourgon Sep 17, 2017
413893d
Selectify works - need to rearrange some idents now
nyarly Sep 17, 2017
c18c922
Updating golden masters so that they build
nyarly Sep 17, 2017
2bd8e4d
Nearly 100% functionality
nyarly Sep 18, 2017
706be2a
Updated masters - all seems to work
nyarly Sep 24, 2017
e49f103
Now testing that everything builds
nyarly Sep 24, 2017
55ba856
Removing AST experiments
nyarly Sep 24, 2017
b225e46
Chopping up long sourcecontext.gog
nyarly Sep 25, 2017
7ce03db
Tiny little notes
nyarly Sep 26, 2017
4c2a022
auth/jwt: add claim factory to example
bcho Sep 26, 2017
492a87e
auth/jwt: minor gofmt fixes
bcho Sep 26, 2017
9ed3743
Merge pull request #617 from bcho/fix-doc/auth-jwt
ChrisHines Sep 26, 2017
c05f7e6
fix typo in addcli
karthikmuralidharan Oct 3, 2017
4dc7be5
Merge pull request #619 from karthikmuralidharan/patch-1
peterbourgon Oct 3, 2017
b1240ac
Cleaning up some type assertion digging
nyarly Oct 8, 2017
3752c99
Remove dependency on juju
nelz9999 Oct 9, 2017
43c4183
Downstream usages of ratelimit package
nelz9999 Oct 10, 2017
e2b2984
Merge pull request #622 from nelz9999/nelz-x-juju
peterbourgon Oct 21, 2017
0f76b6c
Recreating profilesvc issue
nyarly Oct 29, 2017
d3d877a
Adding .ignore for rg
nyarly Oct 29, 2017
d280089
flat layout works with defined types
nyarly Oct 30, 2017
b44e2a5
Default layout mostly works - one remaining selectify issue
nyarly Oct 31, 2017
d7d6d07
Debugging replaces - determined we need to do cloning
nyarly Nov 2, 2017
e98edf9
Halfway through an edit - taking it home
nyarly Nov 4, 2017
40a7aee
Works with new profilesvc testcases
nyarly Nov 5, 2017
e3b2152
Try to fix Thrift failure (again) (#630)
peterbourgon Nov 12, 2017
1c434a3
cmd/kitgen: parse
peterbourgon Jul 3, 2016
74638bd
First stymie
nyarly Jul 15, 2017
4374643
Sketch developing
nyarly Jul 16, 2017
9b2f777
Broken sketch.
nyarly Jul 17, 2017
a6627d0
Further progress - still doesn't build
nyarly Jul 18, 2017
080d80e
Need to collect ast.Exprs, not strings
nyarly Jul 18, 2017
dab9ae7
Running downhill.
nyarly Jul 19, 2017
6246ebb
Fruitful avenue. Committing for travel.
nyarly Jul 24, 2017
4441bc6
Needs uniquify for varnames
nyarly Jul 26, 2017
d4d943a
Service generation tests work
nyarly Jul 31, 2017
00fee2e
Functions extracted from inline codefile
nyarly Aug 7, 2017
c8b3d29
Handles anonymous fields for all parts of interface
nyarly Aug 13, 2017
06d8837
Handles underscore param names, produces compile-able code
nyarly Aug 14, 2017
f76159f
Converted flat to a layout - proceeding to implement default
nyarly Aug 15, 2017
fe75631
Convenience function for formatting to a tree
nyarly Aug 15, 2017
475bc74
Default layout
nyarly Aug 20, 2017
b167bc4
Need to handle mutating trees more effectively to do default layout.
nyarly Aug 22, 2017
1bae153
Replacing idents successfully
nyarly Aug 31, 2017
ba93957
Constructs import paths usefully
nyarly Sep 5, 2017
518b57a
Some debugging - transit
nyarly Sep 11, 2017
ad302ba
Selectify works - need to rearrange some idents now
nyarly Sep 17, 2017
d3bcaa8
Updating golden masters so that they build
nyarly Sep 17, 2017
4c028a1
Nearly 100% functionality
nyarly Sep 18, 2017
73c5884
Updated masters - all seems to work
nyarly Sep 24, 2017
34048c2
Now testing that everything builds
nyarly Sep 24, 2017
07595a6
Removing AST experiments
nyarly Sep 24, 2017
b9210a2
Chopping up long sourcecontext.gog
nyarly Sep 25, 2017
566f9bf
Tiny little notes
nyarly Sep 26, 2017
351ad9f
Cleaning up some type assertion digging
nyarly Oct 8, 2017
0156ebe
Recreating profilesvc issue
nyarly Oct 29, 2017
72c389e
Adding .ignore for rg
nyarly Oct 29, 2017
31848c5
flat layout works with defined types
nyarly Oct 30, 2017
73c8ebc
Default layout mostly works - one remaining selectify issue
nyarly Oct 31, 2017
964b4e7
Debugging replaces - determined we need to do cloning
nyarly Nov 2, 2017
b203a76
Halfway through an edit - taking it home
nyarly Nov 4, 2017
0096b57
Works with new profilesvc testcases
nyarly Nov 5, 2017
627d553
Merge branch 'kitgen' of github.com:nyarly/kit into kitgen
nyarly Dec 1, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ script:
- ./coveralls.bash

go:
- 1.7.x
- 1.8.x
- 1.9.x
- tip
20 changes: 20 additions & 0 deletions auth/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
This package provides a Basic Authentication middleware.

It'll try to compare credentials from Authentication request header to a username/password pair in middleware constructor.

More details about this type of authentication can be found in [Mozilla article](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).

## Usage

```go
import httptransport "github.com/go-kit/kit/transport/http"

httptransport.NewServer(
AuthMiddleware(cfg.auth.user, cfg.auth.password, "Example Realm")(makeUppercaseEndpoint()),
decodeMappingsRequest,
httptransport.EncodeJSONResponse,
httptransport.ServerBefore(httptransport.PopulateRequestContext),
)
```

For AuthMiddleware to be able to pick up the Authentication header from an HTTP request we need to pass it through the context with something like ```httptransport.ServerBefore(httptransport.PopulateRequestContext)```.
94 changes: 94 additions & 0 deletions auth/basic/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package basic

import (
"bytes"
"context"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"fmt"
"net/http"
"strings"

"github.com/go-kit/kit/endpoint"
httptransport "github.com/go-kit/kit/transport/http"
)

// AuthError represents an authorization error.
type AuthError struct {
Realm string
}

// StatusCode is an implementation of the StatusCoder interface in go-kit/http.
func (AuthError) StatusCode() int {
return http.StatusUnauthorized
}

// Error is an implementation of the Error interface.
func (AuthError) Error() string {
return http.StatusText(http.StatusUnauthorized)
}

// Headers is an implementation of the Headerer interface in go-kit/http.
func (e AuthError) Headers() http.Header {
return http.Header{
"Content-Type": []string{"text/plain; charset=utf-8"},
"X-Content-Type-Options": []string{"nosniff"},
"WWW-Authenticate": []string{fmt.Sprintf(`Basic realm=%q`, e.Realm)},
}
}

// parseBasicAuth parses an HTTP Basic Authentication string.
// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ([]byte("Aladdin"), []byte("open sesame"), true).
func parseBasicAuth(auth string) (username, password []byte, ok bool) {
const prefix = "Basic "
if !strings.HasPrefix(auth, prefix) {
return
}
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
if err != nil {
return
}

s := bytes.IndexByte(c, ':')
if s < 0 {
return
}
return c[:s], c[s+1:], true
}

// Returns a hash of a given slice.
func toHashSlice(s []byte) []byte {
hash := sha256.Sum256(s)
return hash[:]
}

// AuthMiddleware returns a Basic Authentication middleware for a particular user and password.
func AuthMiddleware(requiredUser, requiredPassword, realm string) endpoint.Middleware {
requiredUserBytes := toHashSlice([]byte(requiredUser))
requiredPasswordBytes := toHashSlice([]byte(requiredPassword))

return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
auth, ok := ctx.Value(httptransport.ContextKeyRequestAuthorization).(string)
if !ok {
return nil, AuthError{realm}
}

givenUser, givenPassword, ok := parseBasicAuth(auth)
if !ok {
return nil, AuthError{realm}
}

givenUserBytes := toHashSlice(givenUser)
givenPasswordBytes := toHashSlice(givenPassword)

if subtle.ConstantTimeCompare(givenUserBytes, requiredUserBytes) == 0 ||
subtle.ConstantTimeCompare(givenPasswordBytes, requiredPasswordBytes) == 0 {
return nil, AuthError{realm}
}

return next(ctx, request)
}
}
}
52 changes: 52 additions & 0 deletions auth/basic/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package basic

import (
"context"
"encoding/base64"
"fmt"
"testing"

httptransport "github.com/go-kit/kit/transport/http"
)

func TestWithBasicAuth(t *testing.T) {
requiredUser := "test-user"
requiredPassword := "test-pass"
realm := "test realm"

type want struct {
result interface{}
err error
}
tests := []struct {
name string
authHeader interface{}
want want
}{
{"Isn't valid with nil header", nil, want{nil, AuthError{realm}}},
{"Isn't valid with non-string header", 42, want{nil, AuthError{realm}}},
{"Isn't valid without authHeader", "", want{nil, AuthError{realm}}},
{"Isn't valid for wrong user", makeAuthString("wrong-user", requiredPassword), want{nil, AuthError{realm}}},
{"Isn't valid for wrong password", makeAuthString(requiredUser, "wrong-password"), want{nil, AuthError{realm}}},
{"Is valid for correct creds", makeAuthString(requiredUser, requiredPassword), want{true, nil}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.WithValue(context.TODO(), httptransport.ContextKeyRequestAuthorization, tt.authHeader)

result, err := AuthMiddleware(requiredUser, requiredPassword, realm)(passedValidation)(ctx, nil)
if result != tt.want.result || err != tt.want.err {
t.Errorf("WithBasicAuth() = result: %v, err: %v, want result: %v, want error: %v", result, err, tt.want.result, tt.want.err)
}
})
}
}

func makeAuthString(user string, password string) string {
data := []byte(fmt.Sprintf("%s:%s", user, password))
return fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(data))
}

func passedValidation(ctx context.Context, request interface{}) (response interface{}, err error) {
return true, nil
}
14 changes: 7 additions & 7 deletions auth/jwt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ will be added to the context via the `jwt.JWTClaimsContextKey`.
```go
import (
stdjwt "github.com/dgrijalva/jwt-go"

"github.com/go-kit/kit/auth/jwt"
"github.com/go-kit/kit/endpoint"
)
Expand All @@ -23,7 +23,7 @@ func main() {
{
kf := func(token *stdjwt.Token) (interface{}, error) { return []byte("SigningString"), nil }
exampleEndpoint = MakeExampleEndpoint(service)
exampleEndpoint = jwt.NewParser(kf, stdjwt.SigningMethodHS256)(exampleEndpoint)
exampleEndpoint = jwt.NewParser(kf, stdjwt.SigningMethodHS256, jwt.StandardClaimsFactory)(exampleEndpoint)
}
}
```
Expand All @@ -35,7 +35,7 @@ the token string and add it to the context via the `jwt.JWTTokenContextKey`.
```go
import (
stdjwt "github.com/dgrijalva/jwt-go"

"github.com/go-kit/kit/auth/jwt"
"github.com/go-kit/kit/endpoint"
)
Expand All @@ -45,9 +45,9 @@ func main() {
{
exampleEndpoint = grpctransport.NewClient(...).Endpoint()
exampleEndpoint = jwt.NewSigner(
"kid-header",
[]byte("SigningString"),
stdjwt.SigningMethodHS256,
"kid-header",
[]byte("SigningString"),
stdjwt.SigningMethodHS256,
jwt.Claims{},
)(exampleEndpoint)
}
Expand All @@ -67,7 +67,7 @@ Example of use in a client:
import (
stdjwt "github.com/dgrijalva/jwt-go"

grpctransport "github.com/go-kit/kit/transport/grpc"
grpctransport "github.com/go-kit/kit/transport/grpc"
"github.com/go-kit/kit/auth/jwt"
"github.com/go-kit/kit/endpoint"
)
Expand Down
10 changes: 5 additions & 5 deletions auth/jwt/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,21 @@ func NewSigner(kid string, key []byte, method jwt.SigningMethod, claims jwt.Clai
// Useful in NewParser middleware.
type ClaimsFactory func() jwt.Claims

// MapClaimsFactory is a ClaimsFactory that returns
// MapClaimsFactory is a ClaimsFactory that returns
// an empty jwt.MapClaims.
func MapClaimsFactory() jwt.Claims {
return jwt.MapClaims{}
return jwt.MapClaims{}
}

// StandardClaimsFactory is a ClaimsFactory that returns
// StandardClaimsFactory is a ClaimsFactory that returns
// an empty jwt.StandardClaims.
func StandardClaimsFactory() jwt.Claims {
return &jwt.StandardClaims{}
return &jwt.StandardClaims{}
}

// NewParser creates a new JWT token parsing middleware, specifying a
// jwt.Keyfunc interface, the signing method and the claims type to be used. NewParser
// adds the resulting claims to endpoint context or returns error on invalid token.
// adds the resulting claims to endpoint context or returns error on invalid token.
// Particularly useful for servers.
func NewParser(keyFunc jwt.Keyfunc, method jwt.SigningMethod, newClaims ClaimsFactory) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ machine:
pre:
- curl -sSL https://s3.amazonaws.com/circle-downloads/install-circleci-docker.sh | bash -s -- 1.10.0
- sudo rm -rf /usr/local/go
- curl -sSL https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz | sudo tar xz -C /usr/local
- curl -sSL https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz | sudo tar xz -C /usr/local
services:
- docker

Expand Down
1 change: 1 addition & 0 deletions cmd/kitgen/.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/*/*/
36 changes: 36 additions & 0 deletions cmd/kitgen/arg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import "go/ast"

type arg struct {
name, asField *ast.Ident
typ ast.Expr
}

func (a arg) chooseName(scope *ast.Scope) *ast.Ident {
if a.name == nil || scope.Lookup(a.name.Name) != nil {
return inventName(a.typ, scope)
}
return a.name
}

func (a arg) field(scope *ast.Scope) *ast.Field {
return &ast.Field{
Names: []*ast.Ident{a.chooseName(scope)},
Type: a.typ,
}
}

func (a arg) result() *ast.Field {
return &ast.Field{
Names: nil,
Type: a.typ,
}
}

func (a arg) exported() *ast.Field {
return &ast.Field{
Names: []*ast.Ident{id(export(a.asField.Name))},
Type: a.typ,
}
}
Loading