Files
teleport/lib/cloud/azure/postgres.go
T
Gavin FrazarandMarek Smoliński b79a9783ed Azure API for DB discovery (#15674)
* Add Azure auto-discovery configuration fields

* Init databases if azure matchers are in config

* Use AzureMatchers in db service

* Use all azure subscriptions/resource groups if omitted in matcher

* Add azure config tests

* Go mod tidy to update dependencies

* Add azure response error conversion

* Check for azure access denied and give a helpful error message

* Add azure subscriptions api

* Add azure mysql/postgresql api and wrappers

* Test generic db server for azure

* Make server properties its own type

* Convert server types manually instead of via json

* Move server list method selection logic out of api client

* Update azure db server tests

* Fixup merge

* Update comments

* Update more comments and remove junk code

* Move all azure api into lib/cloud/azure

* Update state and version checks

* Add mutex to subscription client for caching, just in case

* Update lib/cloud/azure/db_server_test.go

Co-authored-by: Marek Smoliński <marek@goteleport.com>

* Update lib/cloud/azure/subscriptions_test.go

Co-authored-by: Marek Smoliński <marek@goteleport.com>

* Update lib/cloud/azure/db_server_test.go

Co-authored-by: Marek Smoliński <marek@goteleport.com>

* Update lib/cloud/azure/db_server_test.go

Co-authored-by: Marek Smoliński <marek@goteleport.com>

* Update lib/cloud/azure/db_server_test.go

Co-authored-by: Marek Smoliński <marek@goteleport.com>

* Rename azure subscription client and remove sub ID caching

* Add reference links for azure db ports

* Move indirect dep into group

* Wrap all converted azure response errors

* Remove unreachable panic

* Godoc DBServer

* Remove maxPages arg to azure client funcs

* Gofmt

* Spacing between copyright and package

* import order

Co-authored-by: Marek Smoliński <marek@goteleport.com>
2022-08-25 19:15:05 +00:00

84 lines
2.5 KiB
Go

/*
Copyright 2022 Gravitational, Inc.
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 azure
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/postgresql/armpostgresql"
"github.com/gravitational/trace"
)
var _ DBServersClient = (*postgresClient)(nil)
// postgresClient wraps the ARMPostgres API so we can implement the DBServersClient interface.
type postgresClient struct {
api ARMPostgres
}
// NewPostgresServerClient returns a DBServersClient for Azure PostgreSQL servers.
func NewPostgresServerClient(api ARMPostgres) DBServersClient {
return &postgresClient{api: api}
}
func (c *postgresClient) Get(ctx context.Context, group, name string) (*DBServer, error) {
res, err := c.api.Get(ctx, group, name, nil)
if err != nil {
return nil, trace.Wrap(ConvertResponseError(err))
}
return ServerFromPostgresServer(&res.Server), nil
}
func (c *postgresClient) ListAll(ctx context.Context) ([]*DBServer, error) {
var servers []*DBServer
options := &armpostgresql.ServersClientListOptions{}
pager := c.api.NewListPager(options)
for pageNum := 0; pager.More(); pageNum++ {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, trace.Wrap(ConvertResponseError(err))
}
for _, s := range page.Value {
servers = append(servers, ServerFromPostgresServer(s))
}
}
return servers, nil
}
func (c *postgresClient) ListWithinGroup(ctx context.Context, group string) ([]*DBServer, error) {
var servers []*DBServer
options := &armpostgresql.ServersClientListByResourceGroupOptions{}
pager := c.api.NewListByResourceGroupPager(group, options)
for pageNum := 0; pager.More(); pageNum++ {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, trace.Wrap(ConvertResponseError(err))
}
for _, s := range page.Value {
servers = append(servers, ServerFromPostgresServer(s))
}
}
return servers, nil
}
// IsVersionSupported returns true if database supports AAD authentication.
// All Azure managed PostgreSQL single-server instances support AAD auth.
func isPostgresVersionSupported(s *DBServer) bool {
return true
}