[MM-55028] Added OAuthOutgoingConnection store (#25221)

* OAuthOutgoingConnection model

* added store

* make generated

* add missing license headers

* fix receiver name

* i18n

* i18n sorting

* update migrations from master

* make migrations-extract

* update retrylayer tests

* replaced sql query with id pagination

* fixed flaky tests

* missing columns

* missing columns on save/update

* typo

* improved tests

* remove enum from mysql colum

* add password credentials to store

* renamed migrations

* model change suggestions

* refactor test functionsn

* migration typo

* refactor store table names

* updated sanitize test

* oauthoutgoingconnection -> outgoingoauthconnection

* signature change

* i18n update

* granttype typo

* uppercase typo

* lowercase store name
Этот коммит содержится в:
Felipe Martin
2023-11-20 15:42:07 +01:00
коммит произвёл GitHub
родитель 8158c0e614
Коммит b40366dbdf
20 изменённых файлов: 1419 добавлений и 15 удалений

Просмотреть файл

@@ -0,0 +1,158 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"net/http"
"unicode/utf8"
)
type OutgoingOAuthConnectionGrantType string
func (gt OutgoingOAuthConnectionGrantType) IsValid() bool {
return gt == OutgoingOAuthConnectionGrantTypeClientCredentials || gt == OutgoingOAuthConnectionGrantTypePassword
}
const (
OutgoingOAuthConnectionGrantTypeClientCredentials OutgoingOAuthConnectionGrantType = "client_credentials"
OutgoingOAuthConnectionGrantTypePassword OutgoingOAuthConnectionGrantType = "password"
defaultGetConnectionsLimit = 50
)
type OutgoingOAuthConnection struct {
Id string `json:"id"`
CreatorId string `json:"creator_id"`
CreateAt int64 `json:"create_at"`
UpdateAt int64 `json:"update_at"`
Name string `json:"name"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
CredentialsUsername *string `json:"credentials_username,omitempty"`
CredentialsPassword *string `json:"credentials_password,omitempty"`
OAuthTokenURL string `json:"oauth_token_url"`
GrantType OutgoingOAuthConnectionGrantType `json:"grant_type"`
Audiences StringArray `json:"audiences"`
}
func (oa *OutgoingOAuthConnection) Auditable() map[string]interface{} {
return map[string]interface{}{
"id": oa.Id,
"creator_id": oa.CreatorId,
"create_at": oa.CreateAt,
"update_at": oa.UpdateAt,
"name": oa.Name,
"grant_type": oa.GrantType,
}
}
// IsValid validates the object and returns an error if it isn't properly configured
func (oa *OutgoingOAuthConnection) IsValid() *AppError {
if !IsValidId(oa.Id) {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.id.error", nil, "", http.StatusBadRequest)
}
if oa.CreateAt == 0 {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.create_at.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
if oa.UpdateAt == 0 {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.update_at.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
if !IsValidId(oa.CreatorId) {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.creator_id.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
if utf8.RuneCountInString(oa.Name) > 64 {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.name.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
if len(oa.ClientId) == 0 || utf8.RuneCountInString(oa.ClientId) > 255 {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.client_id.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
if len(oa.ClientSecret) == 0 || utf8.RuneCountInString(oa.ClientSecret) > 255 {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.client_secret.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
if len(oa.OAuthTokenURL) == 0 || utf8.RuneCountInString(oa.OAuthTokenURL) > 256 {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.oauth_token_url.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
if err := oa.IsValidGrantType(); err != nil {
return err
}
if len(oa.Audiences) == 0 {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.audience.empty", nil, "id="+oa.Id, http.StatusBadRequest)
}
if len(oa.Audiences) > 0 {
for _, audience := range oa.Audiences {
if !IsValidHTTPURL(audience) {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.audience.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
}
}
return nil
}
// IsValidGrantType validates the grant type and its parameters returning an error if it isn't properly configured
func (oa *OutgoingOAuthConnection) IsValidGrantType() *AppError {
if !oa.GrantType.IsValid() {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.grant_type.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
if oa.GrantType == OutgoingOAuthConnectionGrantTypePassword && (oa.CredentialsUsername == nil || oa.CredentialsPassword == nil) {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.password_credentials.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
if oa.GrantType == OutgoingOAuthConnectionGrantTypePassword && (*oa.CredentialsUsername == "" || *oa.CredentialsPassword == "") {
return NewAppError("OutgoingOAuthConnection.IsValid", "model.outgoing_oauth_connection.is_valid.password_credentials.error", nil, "id="+oa.Id, http.StatusBadRequest)
}
return nil
}
// PreSave will set the Id if empty, ensuring the object has one and the create/update times.
func (oa *OutgoingOAuthConnection) PreSave() {
if oa.Id == "" {
oa.Id = NewId()
}
oa.CreateAt = GetMillis()
oa.UpdateAt = oa.CreateAt
}
// PreUpdate will set the update time to now.
func (oa *OutgoingOAuthConnection) PreUpdate() {
oa.UpdateAt = GetMillis()
}
// Etag returns the ETag for the cache.
func (oa *OutgoingOAuthConnection) Etag() string {
return Etag(oa.Id, oa.UpdateAt)
}
// Sanitize removes any sensitive fields from the OutgoingOAuthConnection object.
func (oa *OutgoingOAuthConnection) Sanitize() {
oa.ClientSecret = ""
oa.CredentialsUsername = nil
oa.CredentialsPassword = nil
}
// OutgoingOAuthConnectionGetConnectionsFilter is used to filter outgoing connections
type OutgoingOAuthConnectionGetConnectionsFilter struct {
OffsetId string
Limit int
}
// SetDefaults sets the default values for the filter
func (oaf *OutgoingOAuthConnectionGetConnectionsFilter) SetDefaults() {
if oaf.Limit == 0 {
oaf.Limit = defaultGetConnectionsLimit
}
}

Просмотреть файл

@@ -0,0 +1,313 @@
package model
import (
"testing"
"github.com/stretchr/testify/require"
)
var (
emptyString = ""
someString = "userorpass"
)
func newValidOutgoingOAuthConnection() *OutgoingOAuthConnection {
return &OutgoingOAuthConnection{
Id: NewId(),
CreatorId: NewId(),
Name: "Test Connection",
ClientId: NewId(),
ClientSecret: NewId(),
OAuthTokenURL: "https://nowhere.com/oauth/token",
GrantType: "client_credentials",
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
Audiences: []string{"https://nowhere.com"},
}
}
func TestOutgoingOAuthConnectionIsValid(t *testing.T) {
var cases = []struct {
name string
item func() *OutgoingOAuthConnection
assert func(t *testing.T, oa *OutgoingOAuthConnection)
}{
{
name: "valid",
item: func() *OutgoingOAuthConnection {
return newValidOutgoingOAuthConnection()
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Nil(t, oa.IsValid())
},
},
{
name: "invalid id",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.Id = ""
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid create_at",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.CreateAt = 0
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid update_at",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.UpdateAt = 0
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid creator_id",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.CreatorId = ""
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid name",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.Name = ""
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid client_id",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.ClientId = ""
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "long client_id",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.ClientId = string(make([]byte, 257))
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid client_secret",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.ClientSecret = ""
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "long client_secret",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.ClientSecret = string(make([]byte, 257))
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "empty oauth_token_url",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.OAuthTokenURL = ""
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "long oauth_token_url",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.OAuthTokenURL = string(make([]byte, 257))
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid oauth_token_url",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.OAuthTokenURL = "invalid"
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid grant_type",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.GrantType = ""
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "nil password credentials",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.GrantType = OutgoingOAuthConnectionGrantTypePassword
oa.CredentialsUsername = nil
oa.CredentialsPassword = nil
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid password credentials username",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.GrantType = OutgoingOAuthConnectionGrantTypePassword
oa.CredentialsUsername = &emptyString
oa.CredentialsPassword = &someString
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid password credentials password",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.GrantType = OutgoingOAuthConnectionGrantTypePassword
oa.CredentialsUsername = &someString
oa.CredentialsPassword = &emptyString
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "empty password credentials",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.GrantType = OutgoingOAuthConnectionGrantTypePassword
oa.CredentialsUsername = &emptyString
oa.CredentialsPassword = &emptyString
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "correct password credentials",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.GrantType = OutgoingOAuthConnectionGrantTypePassword
oa.CredentialsUsername = &someString
oa.CredentialsPassword = &someString
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Nil(t, oa.IsValid())
},
},
{
name: "empty audience",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.Audiences = []string{}
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
{
name: "invalid audience",
item: func() *OutgoingOAuthConnection {
oa := newValidOutgoingOAuthConnection()
oa.Audiences = []string{"https://nowhere.com", "invalid"}
return oa
},
assert: func(t *testing.T, oa *OutgoingOAuthConnection) {
require.Error(t, oa.IsValid())
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tc.assert(t, tc.item())
})
}
}
func TestOutgoingOAuthConnectionPreSave(t *testing.T) {
oa := newValidOutgoingOAuthConnection()
oa.PreSave()
require.NotEmpty(t, oa.Id)
require.NotZero(t, oa.CreateAt)
require.NotZero(t, oa.UpdateAt)
}
func TestOutgoingOAuthConnectionPreUpdate(t *testing.T) {
oa := newValidOutgoingOAuthConnection()
oa.PreUpdate()
require.NotZero(t, oa.UpdateAt)
}
func TestOutgoingOAuthConnectionEtag(t *testing.T) {
oa := newValidOutgoingOAuthConnection()
oa.PreSave()
require.NotEmpty(t, oa.Etag())
}
func TestOutgoingOAuthConnectionSanitize(t *testing.T) {
oa := newValidOutgoingOAuthConnection()
oa.Sanitize()
require.Empty(t, oa.ClientSecret)
require.Empty(t, oa.CredentialsUsername)
require.Empty(t, oa.CredentialsPassword)
}