Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e6345f04ed | ||
|
|
f03eeefd90 |
@ -2,8 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alcionai/corso/src/cli"
|
"github.com/alcionai/corso/src/cli"
|
||||||
|
prof "github.com/alcionai/corso/src/internal/profile"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
prof.Profiler()
|
||||||
cli.Handle()
|
cli.Handle()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,6 +48,8 @@ require (
|
|||||||
github.com/gofrs/flock v0.8.1 // indirect
|
github.com/gofrs/flock v0.8.1 // indirect
|
||||||
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
|
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
|
||||||
github.com/google/go-cmp v0.5.9 // indirect
|
github.com/google/go-cmp v0.5.9 // indirect
|
||||||
|
github.com/grafana/pyroscope-go v1.0.4 // indirect
|
||||||
|
github.com/grafana/pyroscope-go/godeltaprof v0.1.4 // indirect
|
||||||
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
|
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
|
||||||
github.com/hashicorp/cronexpr v1.1.2 // indirect
|
github.com/hashicorp/cronexpr v1.1.2 // indirect
|
||||||
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
|
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
|
||||||
@ -79,6 +81,7 @@ require (
|
|||||||
github.com/edsrzf/mmap-go v1.1.0 // indirect
|
github.com/edsrzf/mmap-go v1.1.0 // indirect
|
||||||
github.com/go-logr/logr v1.2.4 // indirect
|
github.com/go-logr/logr v1.2.4 // indirect
|
||||||
github.com/go-logr/stdr v1.2.2 // indirect
|
github.com/go-logr/stdr v1.2.2 // indirect
|
||||||
|
github.com/golang-jwt/jwt v3.2.2+incompatible
|
||||||
github.com/golang/protobuf v1.5.3 // indirect
|
github.com/golang/protobuf v1.5.3 // indirect
|
||||||
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
|
|||||||
@ -138,6 +138,8 @@ github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
|
|||||||
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
|
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
|
||||||
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
|
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
|
||||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||||
|
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||||
|
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||||
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
|
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
|
||||||
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||||
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
|
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
|
||||||
@ -207,6 +209,10 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
|
|||||||
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
|
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
|
||||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||||
|
github.com/grafana/pyroscope-go v1.0.4 h1:oyQX0BOkL+iARXzHuCdIF5TQ7/sRSel1YFViMHC7Bm0=
|
||||||
|
github.com/grafana/pyroscope-go v1.0.4/go.mod h1:0d7ftwSMBV/Awm7CCiYmHQEG8Y44Ma3YSjt+nWcWztY=
|
||||||
|
github.com/grafana/pyroscope-go/godeltaprof v0.1.4 h1:mDsJ3ngul7UfrHibGQpV66PbZ3q1T8glz/tK3bQKKEk=
|
||||||
|
github.com/grafana/pyroscope-go/godeltaprof v0.1.4/go.mod h1:1HSPtjU8vLG0jE9JrTdzjgFqdJ/VgN7fvxBNq3luJko=
|
||||||
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
|
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
|
||||||
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y=
|
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y=
|
||||||
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
|
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
|
||||||
|
|||||||
29
src/internal/common/jwt/jwt.go
Normal file
29
src/internal/common/jwt/jwt.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package jwt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/alcionai/clues"
|
||||||
|
jwt "github.com/golang-jwt/jwt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsJWTExpired checks if the JWT token is past expiry by analyzing the
|
||||||
|
// "exp" claim present in the token. Token is considered alive if :
|
||||||
|
// 1. time.now <= "exp" claim.
|
||||||
|
// 2. "exp" claim is missing.
|
||||||
|
// An error is returned if the supplied token is malformed.
|
||||||
|
func IsJWTExpired(
|
||||||
|
rawToken string,
|
||||||
|
) (bool, error) {
|
||||||
|
// Note: Call to ParseUnverified is intentional since token verification is
|
||||||
|
// not our objective. We assume the token signature is valid & verified
|
||||||
|
// by caller stack. We only care about the embed claims in the token.
|
||||||
|
token, _, err := new(jwt.Parser).ParseUnverified(rawToken, jwt.MapClaims{})
|
||||||
|
if err != nil {
|
||||||
|
return false, clues.Wrap(err, "invalid jwt")
|
||||||
|
}
|
||||||
|
|
||||||
|
claims, _ := token.Claims.(jwt.MapClaims)
|
||||||
|
// If "exp" claim is missing, token is considered alive.
|
||||||
|
expired := !claims.VerifyExpiresAt(jwt.TimeFunc().Unix(), false)
|
||||||
|
|
||||||
|
return expired, nil
|
||||||
|
}
|
||||||
99
src/internal/common/jwt/jwt_test.go
Normal file
99
src/internal/common/jwt/jwt_test.go
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package jwt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
jwt "github.com/golang-jwt/jwt"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
|
"github.com/alcionai/corso/src/internal/tester"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JWTUnitSuite struct {
|
||||||
|
tester.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJWTUnitSuite(t *testing.T) {
|
||||||
|
suite.Run(t, &JWTUnitSuite{Suite: tester.NewUnitSuite(t)})
|
||||||
|
}
|
||||||
|
|
||||||
|
// createJWTToken creates a JWT token with the specified expiration time.
|
||||||
|
func createJWTToken(
|
||||||
|
expiration time.Time,
|
||||||
|
claims jwt.MapClaims,
|
||||||
|
) (string, error) {
|
||||||
|
// build claims from map
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
|
||||||
|
return token.SignedString([]byte(""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *JWTUnitSuite) TestIsJWTExpired() {
|
||||||
|
table := []struct {
|
||||||
|
name string
|
||||||
|
expect bool
|
||||||
|
getToken func() (string, error)
|
||||||
|
expectErr assert.ErrorAssertionFunc
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "alive token",
|
||||||
|
getToken: func() (string, error) {
|
||||||
|
return createJWTToken(
|
||||||
|
time.Now().Add(time.Hour),
|
||||||
|
jwt.MapClaims{
|
||||||
|
"exp": time.Now().Add(time.Hour).Unix(),
|
||||||
|
})
|
||||||
|
},
|
||||||
|
expect: false,
|
||||||
|
expectErr: assert.NoError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "expired token",
|
||||||
|
getToken: func() (string, error) {
|
||||||
|
return createJWTToken(
|
||||||
|
time.Now().Add(time.Hour),
|
||||||
|
jwt.MapClaims{
|
||||||
|
"exp": time.Now().Add(-time.Hour).Unix(),
|
||||||
|
})
|
||||||
|
},
|
||||||
|
expect: true,
|
||||||
|
expectErr: assert.NoError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "alive token, missing exp claim",
|
||||||
|
getToken: func() (string, error) {
|
||||||
|
return createJWTToken(time.Now().Add(time.Hour), jwt.MapClaims{})
|
||||||
|
},
|
||||||
|
expect: false,
|
||||||
|
expectErr: assert.NoError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "malformed token",
|
||||||
|
getToken: func() (string, error) {
|
||||||
|
return "header.claims.signature", nil
|
||||||
|
},
|
||||||
|
expect: false,
|
||||||
|
expectErr: assert.Error,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range table {
|
||||||
|
suite.Run(test.name, func() {
|
||||||
|
t := suite.T()
|
||||||
|
|
||||||
|
_, flush := tester.NewContext(t)
|
||||||
|
defer flush()
|
||||||
|
|
||||||
|
token, err := test.getToken()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
expired, err := IsJWTExpired(token)
|
||||||
|
test.expectErr(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, test.expect, expired)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/internal/common/url.go
Normal file
27
src/internal/common/url.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/alcionai/clues"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetQueryParamFromURL parses an URL and returns value of the specified
|
||||||
|
// query parameter.
|
||||||
|
func GetQueryParamFromURL(
|
||||||
|
rawURL, queryParam string,
|
||||||
|
) (string, error) {
|
||||||
|
u, err := url.Parse(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
return "", clues.Wrap(err, "parsing url")
|
||||||
|
}
|
||||||
|
|
||||||
|
qp := u.Query()
|
||||||
|
|
||||||
|
val := qp.Get(queryParam)
|
||||||
|
if len(val) == 0 {
|
||||||
|
return "", clues.New("query param not found").With("query_param", queryParam)
|
||||||
|
}
|
||||||
|
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
72
src/internal/common/url_test.go
Normal file
72
src/internal/common/url_test.go
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package common_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
|
"github.com/alcionai/corso/src/internal/common"
|
||||||
|
"github.com/alcionai/corso/src/internal/tester"
|
||||||
|
)
|
||||||
|
|
||||||
|
type URLUnitSuite struct {
|
||||||
|
tester.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestURLUnitSuite(t *testing.T) {
|
||||||
|
suite.Run(t, &URLUnitSuite{Suite: tester.NewUnitSuite(t)})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *URLUnitSuite) TestGetQueryParamFromURL() {
|
||||||
|
qp := "tempauth"
|
||||||
|
table := []struct {
|
||||||
|
name string
|
||||||
|
rawURL string
|
||||||
|
queryParam string
|
||||||
|
expectedResult string
|
||||||
|
expect assert.ErrorAssertionFunc
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid",
|
||||||
|
rawURL: "http://localhost:8080?" + qp + "=h.c.s&other=val",
|
||||||
|
queryParam: qp,
|
||||||
|
expectedResult: "h.c.s",
|
||||||
|
expect: assert.NoError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "query param not found",
|
||||||
|
rawURL: "http://localhost:8080?other=val",
|
||||||
|
queryParam: qp,
|
||||||
|
expect: assert.Error,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty query param",
|
||||||
|
rawURL: "http://localhost:8080?" + qp + "=h.c.s&other=val",
|
||||||
|
queryParam: "",
|
||||||
|
expect: assert.Error,
|
||||||
|
},
|
||||||
|
// In case of multiple occurrences, the first occurrence of param is returned.
|
||||||
|
{
|
||||||
|
name: "multiple occurrences",
|
||||||
|
rawURL: "http://localhost:8080?" + qp + "=h.c.s&other=val&" + qp + "=h1.c1.s1",
|
||||||
|
queryParam: qp,
|
||||||
|
expectedResult: "h.c.s",
|
||||||
|
expect: assert.NoError,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range table {
|
||||||
|
suite.Run(test.name, func() {
|
||||||
|
t := suite.T()
|
||||||
|
|
||||||
|
_, flush := tester.NewContext(t)
|
||||||
|
defer flush()
|
||||||
|
|
||||||
|
token, err := common.GetQueryParamFromURL(test.rawURL, test.queryParam)
|
||||||
|
test.expect(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, test.expectedResult, token)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
47
src/internal/profile/profile.go
Normal file
47
src/internal/profile/profile.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package profile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/grafana/pyroscope-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Profiler() {
|
||||||
|
// These 2 lines are only required if you're using mutex or block profiling
|
||||||
|
// Read the explanation below for how to set these rates:
|
||||||
|
runtime.SetMutexProfileFraction(5)
|
||||||
|
runtime.SetBlockProfileRate(5)
|
||||||
|
|
||||||
|
_, err := pyroscope.Start(pyroscope.Config{
|
||||||
|
ApplicationName: "corso",
|
||||||
|
|
||||||
|
// replace this with the address of pyroscope server
|
||||||
|
ServerAddress: "http://localhost:4040",
|
||||||
|
|
||||||
|
// you can disable logging by setting this to nil
|
||||||
|
Logger: pyroscope.StandardLogger,
|
||||||
|
|
||||||
|
// you can provide static tags via a map:
|
||||||
|
Tags: map[string]string{"hostname": os.Getenv("HOSTNAME")},
|
||||||
|
|
||||||
|
ProfileTypes: []pyroscope.ProfileType{
|
||||||
|
// these profile types are enabled by default:
|
||||||
|
pyroscope.ProfileCPU,
|
||||||
|
pyroscope.ProfileAllocObjects,
|
||||||
|
pyroscope.ProfileAllocSpace,
|
||||||
|
pyroscope.ProfileInuseObjects,
|
||||||
|
pyroscope.ProfileInuseSpace,
|
||||||
|
|
||||||
|
// these profile types are optional:
|
||||||
|
pyroscope.ProfileGoroutines,
|
||||||
|
pyroscope.ProfileMutexCount,
|
||||||
|
pyroscope.ProfileMutexDuration,
|
||||||
|
pyroscope.ProfileBlockCount,
|
||||||
|
pyroscope.ProfileBlockDuration,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user