diff --git a/Makefile b/Makefile index 385eeac..614a78a 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ GOCMD=GO111MODULE=on GOARCH=amd64 go GOBUILD=$(GOCMD) build -v #-mod=vendor GOTEST=$(GOCMD) test -v GOHOSTOS=$(strip $(shell $(GOCMD) env get GOHOSTOS)) +MOCKGEN=$(shell command -v mockgen 2> /dev/null) # Private repo workaround export GOPRIVATE = github.com/microsoft @@ -15,8 +16,8 @@ PKG := all: format test unittest -.PHONY: vendor -vendor: +.PHONY: tidy +tidy: go mod tidy format: @@ -26,19 +27,27 @@ bootstrap: GOOS="linux" go get -u google.golang.org/grpc@v1.26.0 GOOS="linux" go install github.com/golang/protobuf/protoc-gen-go@v1.3.2 -test: - GOOS=windows go build ./... +test: unittest unittest: - $(GOTEST) ./pkg/marshal - $(GOTEST) ./pkg/config - $(GOTEST) ./pkg/tags - $(GOTEST) ./pkg/net - $(GOTEST) ./pkg/certs - $(GOTEST) ./pkg/auth + $(GOTEST) ./... generate: bootstrap (./gen.sh) pipeline: bootstrap (./gen.sh -c) + + +## Install mockgen golang bin +install-mockgen: +ifeq ($(MOCKGEN),) + go install github.com/golang/mock/mockgen@v1.6.0 +endif + MOCKGEN=$(shell command -v mockgen 2> /dev/null) + +mocks: + go mod download github.com/golang/mock + go get github.com/golang/mock@v1.6.0 + go generate ./... + diff --git a/go.mod b/go.mod index 1ab3e29..c8d52e6 100644 --- a/go.mod +++ b/go.mod @@ -13,14 +13,15 @@ require ( ) require ( + github.com/golang/mock v1.6.0 github.com/kr/pretty v0.1.0 // indirect golang.org/x/text v0.8.0 // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect - google.golang.org/protobuf v1.29.1 // indirect + google.golang.org/protobuf v1.30.0 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) replace ( + github.com/golang/mock => github.com/golang/mock v1.6.0 github.com/golang/protobuf/protoc-gen-go => github.com/golang/protobuf/protoc-gen-go v1.3.2 golang.org/x/net => golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c golang.org/x/sys => golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 diff --git a/go.sum b/go.sum index 575a75e..f4b50c8 100644 --- a/go.sum +++ b/go.sum @@ -5,7 +5,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= @@ -32,10 +33,13 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c h1:JVAXQ10yGGVbSyoer5VILysz6YKjdNT2bsvlayjqhes= @@ -43,6 +47,7 @@ golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c/go.mod h1:YDH+HFinaLZZlnHAfS golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 h1:TyKJRhyo17yWxOMCTHKWrc5rddHORMlnZ/j57umaUd8= @@ -57,9 +62,11 @@ golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -70,8 +77,8 @@ google.golang.org/grpc v1.26.0 h1:2dTRdpdFEEhJYQD8EMLB61nnrzSCTbG38PhqdhvOltg= google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62UoM= -google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= +google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 59a0302..e6a3c3f 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -3,6 +3,7 @@ package auth +//go:generate mockgen -destination mock/auth_mock.go github.com/microsoft/moc/pkg/auth Authorizer import ( context "context" "crypto/tls" diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index aa63a91..c2ad246 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -15,6 +15,9 @@ import ( "github.com/microsoft/moc/pkg/certs" "github.com/microsoft/moc/pkg/errors" + + gomock "github.com/golang/mock/gomock" + mock "github.com/microsoft/moc/pkg/auth/mock" ) var key *rsa.PrivateKey @@ -320,3 +323,12 @@ func Test_CertCheckEmpty(t *testing.T) { t.Errorf("certCheck Expected:InvalidInput Actual:%v", err) } } + +func Test_Authorizer_WithRPCAuthorization(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + m := mock.NewMockAuthorizer(ctrl) + m.EXPECT().WithRPCAuthorization() + m.WithRPCAuthorization() +} diff --git a/pkg/auth/mock/auth_mock.go b/pkg/auth/mock/auth_mock.go new file mode 100644 index 0000000..76a4e78 --- /dev/null +++ b/pkg/auth/mock/auth_mock.go @@ -0,0 +1,63 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/microsoft/moc/pkg/auth (interfaces: Authorizer) + +// Package mock_auth is a generated GoMock package. +package mock_auth + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + credentials "google.golang.org/grpc/credentials" +) + +// MockAuthorizer is a mock of Authorizer interface. +type MockAuthorizer struct { + ctrl *gomock.Controller + recorder *MockAuthorizerMockRecorder +} + +// MockAuthorizerMockRecorder is the mock recorder for MockAuthorizer. +type MockAuthorizerMockRecorder struct { + mock *MockAuthorizer +} + +// NewMockAuthorizer creates a new mock instance. +func NewMockAuthorizer(ctrl *gomock.Controller) *MockAuthorizer { + mock := &MockAuthorizer{ctrl: ctrl} + mock.recorder = &MockAuthorizerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAuthorizer) EXPECT() *MockAuthorizerMockRecorder { + return m.recorder +} + +// WithRPCAuthorization mocks base method. +func (m *MockAuthorizer) WithRPCAuthorization() credentials.PerRPCCredentials { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WithRPCAuthorization") + ret0, _ := ret[0].(credentials.PerRPCCredentials) + return ret0 +} + +// WithRPCAuthorization indicates an expected call of WithRPCAuthorization. +func (mr *MockAuthorizerMockRecorder) WithRPCAuthorization() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithRPCAuthorization", reflect.TypeOf((*MockAuthorizer)(nil).WithRPCAuthorization)) +} + +// WithTransportAuthorization mocks base method. +func (m *MockAuthorizer) WithTransportAuthorization() credentials.TransportCredentials { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WithTransportAuthorization") + ret0, _ := ret[0].(credentials.TransportCredentials) + return ret0 +} + +// WithTransportAuthorization indicates an expected call of WithTransportAuthorization. +func (mr *MockAuthorizerMockRecorder) WithTransportAuthorization() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithTransportAuthorization", reflect.TypeOf((*MockAuthorizer)(nil).WithTransportAuthorization)) +} diff --git a/pkg/certs/certificateAuthority.go b/pkg/certs/certificateAuthority.go index 274b31a..9c12c87 100644 --- a/pkg/certs/certificateAuthority.go +++ b/pkg/certs/certificateAuthority.go @@ -2,6 +2,8 @@ // Licensed under the Apache v2.0 license. package certs +//go:generate mockgen -destination mock/mock_certificateAuthority.go github.com/microsoft/moc/pkg/certs Revocation + import ( "bytes" "crypto/rand" diff --git a/pkg/certs/certs_test.go b/pkg/certs/certs_test.go index 5298c0e..af03608 100644 --- a/pkg/certs/certs_test.go +++ b/pkg/certs/certs_test.go @@ -16,6 +16,9 @@ import ( "time" "github.com/microsoft/moc/pkg/errors" + + gomock "github.com/golang/mock/gomock" + mock "github.com/microsoft/moc/pkg/certs/mock" ) func createTestCertificate(before, after time.Time) (string, error) { @@ -875,3 +878,13 @@ func Test_CalculateCertExpiry1(t *testing.T) { t.Errorf("Certificate not expired") } } + +func Test_Revocation_IsRevoked(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + ca, _, _ := GenerateClientCertificate("test CA") + m := mock.NewMockRevocation(ctrl) + m.EXPECT().IsRevoked(ca) + m.IsRevoked(ca) +} diff --git a/pkg/certs/mock/mock_certificateAuthority.go b/pkg/certs/mock/mock_certificateAuthority.go new file mode 100644 index 0000000..96d1f14 --- /dev/null +++ b/pkg/certs/mock/mock_certificateAuthority.go @@ -0,0 +1,49 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/microsoft/moc/pkg/certs (interfaces: Revocation) + +// Package mock_certs is a generated GoMock package. +package mock_certs + +import ( + x509 "crypto/x509" + reflect "reflect" + + gomock "github.com/golang/mock/gomock" +) + +// MockRevocation is a mock of Revocation interface. +type MockRevocation struct { + ctrl *gomock.Controller + recorder *MockRevocationMockRecorder +} + +// MockRevocationMockRecorder is the mock recorder for MockRevocation. +type MockRevocationMockRecorder struct { + mock *MockRevocation +} + +// NewMockRevocation creates a new mock instance. +func NewMockRevocation(ctrl *gomock.Controller) *MockRevocation { + mock := &MockRevocation{ctrl: ctrl} + mock.recorder = &MockRevocationMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockRevocation) EXPECT() *MockRevocationMockRecorder { + return m.recorder +} + +// IsRevoked mocks base method. +func (m *MockRevocation) IsRevoked(arg0 *x509.Certificate) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IsRevoked", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// IsRevoked indicates an expected call of IsRevoked. +func (mr *MockRevocationMockRecorder) IsRevoked(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsRevoked", reflect.TypeOf((*MockRevocation)(nil).IsRevoked), arg0) +} diff --git a/pkg/redact/redact_test.go b/pkg/redact/redact_test.go index a23f9c6..a9e5892 100644 --- a/pkg/redact/redact_test.go +++ b/pkg/redact/redact_test.go @@ -46,6 +46,8 @@ func Test_RedactedMessage(t *testing.T) { Name: "testIdentity", Id: "123", ResourceGroup: "testGroup", + Password: "** Redacted **", + Token: "** Redacted **", LocationName: "testLocation", TokenExpiry: 30, ClientType: common.ClientType_ADMIN,