Skip to content
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

Feature/horusec auth #62

Merged
merged 91 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
03fd80e
Adding horusec auth base project
nathanmartinszup Oct 14, 2020
6351fb1
Adding auth handler, with auth types enum and credetials
nathanmartinszup Oct 15, 2020
01b16a4
Finishing handlers and adding auth controller with factory by type
nathanmartinszup Oct 15, 2020
f0af6a1
Merge branch 'develop' into feature/horusec-auth
nathanmartinszup Oct 15, 2020
7b4640a
Merge branch 'develop' of github.com:ZupIT/horusec into feature/horus…
wiliansilvazup Oct 15, 2020
0653cb8
Change filter to receive Severity and remove Type
wiliansilvazup Oct 15, 2020
a945f02
Adding missing unity tests in devkit
nathanmartinszup Oct 15, 2020
d1b26c4
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
nathanmartinszup Oct 15, 2020
237137a
Adding auth handler tests
nathanmartinszup Oct 15, 2020
86112ee
Adding swagger, updating router and configs
nathanmartinszup Oct 16, 2020
64a18c2
Fixing lint and tests
nathanmartinszup Oct 16, 2020
d1d5e6d
Updating branch with develop
nathanmartinszup Oct 19, 2020
1b73c56
Adding horus service authenticate method
nathanmartinszup Oct 19, 2020
862a44f
Adding authorize handler
nathanmartinszup Oct 19, 2020
4f80c28
Updating auth interface
nathanmartinszup Oct 19, 2020
c3ebdee
Adding postgres read
nathanmartinszup Oct 19, 2020
c4ad36c
Adding keycloak service auth and keycloak shared service
wiliansilvazup Oct 19, 2020
7feab59
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
wiliansilvazup Oct 19, 2020
67e6f9d
Adding unit test
wiliansilvazup Oct 19, 2020
a3c7f0f
Adding horusec roles enum
nathanmartinszup Oct 19, 2020
a7e09b8
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
nathanmartinszup Oct 19, 2020
bf97996
Fixing return
wiliansilvazup Oct 19, 2020
653ac6e
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
wiliansilvazup Oct 19, 2020
abcc618
Adding validation to authorize by horus roles
nathanmartinszup Oct 19, 2020
791b58d
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
nathanmartinszup Oct 19, 2020
2fe07db
Renaming packges to horusec
nathanmartinszup Oct 19, 2020
418006e
Fixing some horusec name errors and unity tests
nathanmartinszup Oct 19, 2020
dc16b9b
Removing nolint and improving code
nathanmartinszup Oct 19, 2020
5ca6c80
Renaming file to horusec
nathanmartinszup Oct 19, 2020
7754933
Adding create user from keycloak token
wiliansilvazup Oct 20, 2020
35d24ee
Adding create user from keycloak token
wiliansilvazup Oct 20, 2020
18ce03c
Adding create user from keycloak token
wiliansilvazup Oct 20, 2020
820526f
Adding horusec service unity testes
nathanmartinszup Oct 20, 2020
2fb56bf
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
nathanmartinszup Oct 20, 2020
0d72cfe
Adding auth controller unity tests and updating mocks
nathanmartinszup Oct 20, 2020
042caca
Adding auth in compose and fixing docs
wiliansilvazup Oct 20, 2020
f71e9df
Adding auth in compose and fixing docs
wiliansilvazup Oct 20, 2020
a8b010b
Adding auth in compose and fixing docs
wiliansilvazup Oct 20, 2020
a9e8ee7
Updating health check
nathanmartinszup Oct 20, 2020
8e37e9f
Fixing lint
wiliansilvazup Oct 20, 2020
daedad5
Updating branch with origin
nathanmartinszup Oct 20, 2020
9cf6473
Fixing keycloak unity tests
nathanmartinszup Oct 20, 2020
8819faa
Adding unit tests
wiliansilvazup Oct 20, 2020
93d6613
Adding unit tests
wiliansilvazup Oct 20, 2020
c3c4898
Adding unit tests
wiliansilvazup Oct 20, 2020
ae5c7ed
Updating middlewares to use auth service
nathanmartinszup Oct 20, 2020
fc59c6a
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
nathanmartinszup Oct 20, 2020
8da72ae
Fixing auth type
wiliansilvazup Oct 20, 2020
d0946eb
Fixing auth type
wiliansilvazup Oct 20, 2020
bd690d4
Fixing tests fmt lint
wiliansilvazup Oct 20, 2020
c74ef97
Fixing tests fmt lint
wiliansilvazup Oct 20, 2020
ae1b490
Fixing Security
wiliansilvazup Oct 20, 2020
88849f0
Improving code and adding unity tests
nathanmartinszup Oct 20, 2020
4edbb55
Adding more devkit unity tests
nathanmartinszup Oct 20, 2020
b60d16c
Adding some unit tests
wiliansilvazup Oct 20, 2020
d81e826
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
wiliansilvazup Oct 20, 2020
f063e50
Adding middleware service unity tests
nathanmartinszup Oct 20, 2020
6b19918
Removing unnecessary test
nathanmartinszup Oct 20, 2020
435d34c
Adding horusec auth readme
nathanmartinszup Oct 21, 2020
0cf9d59
Fixing dockerfiles
wiliansilvazup Oct 21, 2020
a17554d
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
wiliansilvazup Oct 21, 2020
9d4501a
Adding validation to actual auth type
nathanmartinszup Oct 21, 2020
c156e30
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
nathanmartinszup Oct 21, 2020
5fd7eb2
Removing auth type header
nathanmartinszup Oct 21, 2020
b29ccd0
Updating composes
nathanmartinszup Oct 21, 2020
2df1b74
Removing groups from authorization data
nathanmartinszup Oct 21, 2020
f46cbd7
Updating account, api and analytic readme
nathanmartinszup Oct 21, 2020
8db200f
Updating compose with auth url env var
nathanmartinszup Oct 21, 2020
897a739
Updating compose and compose dev
nathanmartinszup Oct 21, 2020
7f8117b
Fixing unity tests and fmt errors
nathanmartinszup Oct 21, 2020
1a85655
Merge branch 'develop' of github.com:ZupIT/horusec into feature/horus…
wiliansilvazup Oct 21, 2020
7de4698
Fixing conflicts
wiliansilvazup Oct 22, 2020
9d5e06a
Merge branch 'master' of github.com:ZupIT/horusec into feature/horuse…
wiliansilvazup Oct 22, 2020
7d45b41
Fixing auth pipeline and hashes false positives
wiliansilvazup Oct 23, 2020
c10d1f9
Fixing error that token was static to accept only jwt
nathanmartinszup Oct 23, 2020
f459af9
Addding role validation in keycloak
nathanmartinszup Oct 23, 2020
3da94ee
Fixing token size and swagger error
nathanmartinszup Oct 23, 2020
453fc82
Removing bearer from keycloak token
nathanmartinszup Oct 23, 2020
28eb85d
Adding api to get account id by token and auth type
nathanmartinszup Oct 26, 2020
a985955
Chaging create account from keycloak to auth
nathanmartinszup Oct 26, 2020
30005cc
Updating auth swagger
nathanmartinszup Oct 26, 2020
a40502b
Updating branch with develop and fixing errors
nathanmartinszup Oct 26, 2020
7841c33
Merge with develop into feature/horusec-auth
wiliansilvazup Oct 26, 2020
a59fd9e
Merge branch 'feature/horusec-auth' of github.com:ZupIT/horusec into …
wiliansilvazup Oct 26, 2020
3a86c2f
Improving keycloak devkit service and fixing tests
nathanmartinszup Oct 26, 2020
ddb510d
Fixing account unity tests
nathanmartinszup Oct 26, 2020
4de28f4
Fixing account unity tests in auth
nathanmartinszup Oct 26, 2020
3b9cdf6
Adding auth unity tests
nathanmartinszup Oct 26, 2020
39c3800
Fixing middleware tests
nathanmartinszup Oct 26, 2020
f55d8a6
Fixing fmt error
nathanmartinszup Oct 26, 2020
eb304ad
Improving interface convertion to avoid conversion error
nathanmartinszup Oct 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
29 changes: 29 additions & 0 deletions development-kit/pkg/entities/auth/authorization_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package auth

import validation "github.com/go-ozzo/ozzo-validation/v4"

type AuthorizationData struct {
Token string
Groups []string
}

func (a *AuthorizationData) Validate() error {
return validation.ValidateStruct(a,
validation.Field(&a.Token, validation.Required, validation.Length(1, 500), validation.Required),
validation.Field(&a.Groups, validation.NotNil, validation.Required),
)
}
48 changes: 48 additions & 0 deletions development-kit/pkg/entities/auth/authorization_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package auth

import (
accountEntities "github.com/ZupIT/horusec/development-kit/pkg/entities/account"
"github.com/ZupIT/horusec/development-kit/pkg/services/jwt"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"testing"
)

func TestAuthorizationDataValidate(t *testing.T) {
t.Run("should return no error when valid data", func(t *testing.T) {
token, _, _ := jwt.CreateToken(&accountEntities.Account{
AccountID: uuid.New(),
Email: "test@test.com",
Password: "safePassword!123",
Username: "test",
IsConfirmed: false,
}, map[string]string{"role": "admin"})

authorizationData := &AuthorizationData{
Token: token,
Groups: []string{"admin"},
}

assert.NoError(t, authorizationData.Validate())
})

t.Run("should return error when invalid data", func(t *testing.T) {
authorizationData := &AuthorizationData{}

assert.Error(t, authorizationData.Validate())
})
}
32 changes: 32 additions & 0 deletions development-kit/pkg/entities/auth/credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package auth

import (
validation "github.com/go-ozzo/ozzo-validation/v4"
)

type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
Otp string `json:"otp"`
}

func (c *Credentials) Validate() error {
return validation.ValidateStruct(c,
validation.Field(&c.Username, validation.Required, validation.Length(1, 255), validation.Required),
validation.Field(&c.Password, validation.Length(1, 255), validation.Required),
)
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
// Copyright 2015 go-swagger maintainers
// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package swag
package auth

import (
"net"
"strconv"
"github.com/stretchr/testify/assert"
"testing"
)

// SplitHostPort splits a network address into a host and a port.
// The port is -1 when there is no port to be found
func SplitHostPort(addr string) (host string, port int, err error) {
h, p, err := net.SplitHostPort(addr)
if err != nil {
return "", -1, err
}
if p == "" {
return "", -1, &net.AddrError{Err: "missing port in address", Addr: addr}
}
func TestValidateCredentials(t *testing.T) {
t.Run("should return no error when valid data", func(t *testing.T) {
credentials := &Credentials{
Username: "horus@test.com",
Password: "UltraSafePass",
}

pi, err := strconv.Atoi(p)
if err != nil {
return "", -1, err
}
return h, pi, nil
assert.NoError(t, credentials.Validate())
})

t.Run("should return error when invalid data", func(t *testing.T) {
credentials := &Credentials{}

assert.Error(t, credentials.Validate())
})
}
43 changes: 43 additions & 0 deletions development-kit/pkg/enums/auth/horus_roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package auth

type HorusRoles string

const (
CompanyMember HorusRoles = "companyMember"
CompanyAdmin HorusRoles = "companyAdmin"
RepositoryMember HorusRoles = "repositoryMember"
RepositorySupervisor HorusRoles = "repositorySupervisor"
RepositoryAdmin HorusRoles = "repositoryAdmin"
)

func (h HorusRoles) IsInvalid() bool {
for _, v := range h.Values() {
if v == h {
return false
}
}

return true
}

func (h HorusRoles) Values() []HorusRoles {
return []HorusRoles{
CompanyMember,
CompanyAdmin,
RepositoryMember,
RepositorySupervisor,
RepositoryAdmin,
}
}

func (h HorusRoles) IsEqual(value string) bool {
if value == h.ToString() {
return true
}

return false
}

func (h HorusRoles) ToString() string {
return string(h)
}
49 changes: 49 additions & 0 deletions development-kit/pkg/enums/auth/horus_roles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package auth

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestIsInvalidRoles(t *testing.T) {
t.Run("should true when invalid role", func(t *testing.T) {
testType := HorusRoles("test")
assert.True(t, testType.IsInvalid())
})

t.Run("should false when valid role", func(t *testing.T) {
testType := HorusRoles("companyMember")
assert.False(t, testType.IsInvalid())

testType = "companyAdmin"
assert.False(t, testType.IsInvalid())

testType = "repositoryMember"
assert.False(t, testType.IsInvalid())

testType = "repositorySupervisor"
assert.False(t, testType.IsInvalid())

testType = "repositoryAdmin"
assert.False(t, testType.IsInvalid())
})
}

func TestValuesRoles(t *testing.T) {
t.Run("should 5 valid horus roles", func(t *testing.T) {
testRole := RepositoryAdmin
assert.Len(t, testRole.Values(), 5)
})
}

func TestIsEqual(t *testing.T) {
t.Run("should return true when equal", func(t *testing.T) {
testRole := RepositoryAdmin
assert.True(t, testRole.IsEqual(testRole.ToString()))
})

t.Run("should return false when equal", func(t *testing.T) {
testRole := RepositoryAdmin
assert.False(t, testRole.IsEqual("test"))
})
}
41 changes: 41 additions & 0 deletions development-kit/pkg/enums/auth/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package auth

type AuthorizationType string

const (
Keycloak AuthorizationType = "keycloak"
Ldap AuthorizationType = "ldap"
Horus AuthorizationType = "horus"
nathannascimentozup marked this conversation as resolved.
Show resolved Hide resolved
)

func (a AuthorizationType) IsInvalid() bool {
for _, v := range a.Values() {
if v == a {
return false
}
}

return true
}

func (a AuthorizationType) Values() []AuthorizationType {
return []AuthorizationType{
Keycloak,
Ldap,
Horus,
}
}
45 changes: 45 additions & 0 deletions development-kit/pkg/enums/auth/type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package auth

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestIsInvalid(t *testing.T) {
t.Run("should true when invalid type", func(t *testing.T) {
testType := AuthorizationType("test")
assert.True(t, testType.IsInvalid())
})

t.Run("should false when valid type", func(t *testing.T) {
testType := AuthorizationType("keycloak")
assert.False(t, testType.IsInvalid())

testType = "ldap"
assert.False(t, testType.IsInvalid())

testType = "horus"
assert.False(t, testType.IsInvalid())
})
}

func TestValues(t *testing.T) {
t.Run("should 3 valid auth types", func(t *testing.T) {
testType := Ldap
assert.Len(t, testType.Values(), 3)
})
}
5 changes: 5 additions & 0 deletions development-kit/pkg/enums/errors/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package errors

import "errors"

var ErrorInvalidAuthType = errors.New("{ACCOUNT} invalid auth type, should be ldap, keycloak or horus")
Loading