-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from greenbone/AT-521-enable-authentication-f…
…or-opensearch At 521 enable authentication for opensearch
- Loading branch information
Showing
9 changed files
with
292 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,3 @@ linters: | |
- goimports | ||
- gosec | ||
- errorlint | ||
- wrapcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// SPDX-FileCopyrightText: 2024 Greenbone AG | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package client | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/Nerzal/gocloak/v12" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type KeycloakJWTReceiverCachedInMemory struct { | ||
keycloakRepository IKeycloakRepository | ||
mutex sync.Mutex | ||
cachedToken *gocloak.JWT | ||
} | ||
|
||
func NewKeycloakJWTReceiverCachedInMemory(keycloakRepository IKeycloakRepository) *KeycloakJWTReceiverCachedInMemory { | ||
return &KeycloakJWTReceiverCachedInMemory{ | ||
keycloakRepository: keycloakRepository, | ||
} | ||
} | ||
|
||
func isTokenValid(token *gocloak.JWT) bool { | ||
if token == nil { | ||
return false | ||
} | ||
|
||
parser := jwt.NewParser() | ||
claims := &jwt.MapClaims{} | ||
|
||
_, _, err := parser.ParseUnverified(token.AccessToken, claims) | ||
if err != nil { | ||
log.Error().Msgf("couldn't parse JWT access token: %v", err) | ||
return false | ||
} | ||
|
||
err = jwt.NewValidator( | ||
jwt.WithIssuedAt(), | ||
jwt.WithExpirationRequired(), | ||
).Validate(claims) | ||
if err != nil { | ||
log.Debug().Msgf("JWT access token is invalid: %v", err) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (k *KeycloakJWTReceiverCachedInMemory) getClientToken(clientName, clientSecret string) (*gocloak.JWT, error) { | ||
k.mutex.Lock() | ||
defer k.mutex.Unlock() | ||
|
||
if k.cachedToken == nil || !isTokenValid(k.cachedToken) { | ||
token, err := k.keycloakRepository.getClientToken(clientName, clientSecret) | ||
if err != nil { | ||
return nil, fmt.Errorf("couldn't fetch JWT access token: %w", err) | ||
} | ||
k.cachedToken = token | ||
} | ||
|
||
return k.cachedToken, nil | ||
} | ||
|
||
func (k *KeycloakJWTReceiverCachedInMemory) GetClientAccessToken(clientName, clientSecret string) (string, error) { | ||
token, err := k.getClientToken(clientName, clientSecret) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return token.AccessToken, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// SPDX-FileCopyrightText: 2024 Greenbone AG | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package client | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Nerzal/gocloak/v12" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockKeycloakRepository struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockKeycloakRepository) getClientToken(clientName, clientSecret string) (*gocloak.JWT, error) { | ||
args := m.Called() | ||
return args.Get(0).(*gocloak.JWT), args.Error(1) | ||
} | ||
|
||
func TestKeycloakJWTReceiverCachedInMemory_GetClientToken(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
cachedToken *gocloak.JWT | ||
mockToken *gocloak.JWT | ||
mockError error | ||
expectedToken *gocloak.JWT | ||
expectedError error | ||
shouldFetchToken bool | ||
}{ | ||
{ | ||
name: "No cached token", | ||
cachedToken: nil, | ||
mockToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedError: nil, | ||
shouldFetchToken: true, | ||
}, | ||
{ | ||
name: "invalid cached token", | ||
cachedToken: &gocloak.JWT{ | ||
AccessToken: "not a valid token", | ||
}, | ||
mockToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedError: nil, | ||
shouldFetchToken: true, | ||
}, | ||
{ | ||
name: "Expired cached token", | ||
cachedToken: &gocloak.JWT{ | ||
AccessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE1MTYyMzEwMjJ9.hsfQPY3ZVrVIV-bzI54NRoTDG6wWzORVp68lxGa3D08", | ||
}, | ||
mockToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedError: nil, | ||
shouldFetchToken: true, | ||
}, | ||
{ | ||
name: "NotBefore date is in the future", | ||
cachedToken: &gocloak.JWT{ | ||
AccessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwibmJmIjo0ODczMjQyNTg3LCJleHAiOjQ4NzQyNDI1ODd9.QZeQwoWl-HRbCcuZbt_3DFnA_h-zD5DhPmcBR0TyrQw", | ||
}, | ||
mockToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedError: nil, | ||
shouldFetchToken: true, | ||
}, | ||
{ | ||
name: "IssuedAt date is in the future", | ||
cachedToken: &gocloak.JWT{ | ||
AccessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0Ijo0ODczMjQyNTg3LCJleHAiOjQ4NzQyNDI1ODd9.h63qP0fMQGgx5S8eV-EHEO1zgSlBmjX3xR80iXnvhX0", | ||
}, | ||
mockToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedError: nil, | ||
shouldFetchToken: true, | ||
}, | ||
{ | ||
name: "Valid cached token", | ||
cachedToken: &gocloak.JWT{ | ||
AccessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjQ4NzMyNDI1ODd9.BHuBKDS9MUC01jmo_p4AcVChkbV0aiDZBXcU-hpj8mg", | ||
}, | ||
mockToken: &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
}, | ||
expectedToken: &gocloak.JWT{ | ||
AccessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjQ4NzMyNDI1ODd9.BHuBKDS9MUC01jmo_p4AcVChkbV0aiDZBXcU-hpj8mg", | ||
}, | ||
expectedError: nil, | ||
shouldFetchToken: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
mockTokenReceiver := new(MockKeycloakRepository) | ||
cache := NewKeycloakJWTReceiverCachedInMemory(mockTokenReceiver) | ||
|
||
cache.cachedToken = tc.cachedToken | ||
|
||
mockTokenReceiver.On("getClientToken").Return(tc.mockToken, tc.mockError) | ||
|
||
token, err := cache.getClientToken("testClient", "testSecret") | ||
|
||
assert.ErrorIs(t, err, tc.expectedError) | ||
|
||
assert.Equal(t, tc.expectedToken, token) | ||
if tc.shouldFetchToken { | ||
mockTokenReceiver.AssertCalled(t, "getClientToken") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestKeycloakJWTReceiverCachedInMemory_GetClientAccessToken(t *testing.T) { | ||
mockKeycloakRepository := new(MockKeycloakRepository) | ||
mockToken := &gocloak.JWT{ | ||
AccessToken: "test_token", | ||
ExpiresIn: 3600, | ||
} | ||
|
||
mockKeycloakRepository.On("getClientToken").Return(mockToken, nil) | ||
|
||
cache := NewKeycloakJWTReceiverCachedInMemory(mockKeycloakRepository) | ||
|
||
accessToken, err := cache.GetClientAccessToken("testClient", "testSecret") | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "test_token", accessToken) | ||
mockKeycloakRepository.AssertCalled(t, "getClientToken") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-FileCopyrightText: 2024 Greenbone AG | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Nerzal/gocloak/v12" | ||
) | ||
|
||
type IKeycloakRepository interface { | ||
getClientToken(clientName, clientSecret string) (*gocloak.JWT, error) | ||
// Append keycloak functions here | ||
} | ||
|
||
type KeycloakRepository struct { | ||
client *gocloak.GoCloak | ||
realm string | ||
} | ||
|
||
var _ IKeycloakRepository = &KeycloakRepository{} | ||
|
||
func NewKeycloakRepository(basePath, realm string) *KeycloakRepository { | ||
return &KeycloakRepository{ | ||
client: gocloak.NewClient(basePath), | ||
realm: realm, | ||
} | ||
} | ||
|
||
func (r *KeycloakRepository) getClientToken(clientName, clientSecret string) (*gocloak.JWT, error) { | ||
return r.client.LoginClient(context.Background(), clientName, clientSecret, r.realm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.