teleport/integrations/access/common/config.go
Edward Dowling f07b291d44
Add opsgenie plugin (#25995)
* Move opsgenie client code to integrations/access

* Add initial opsgenie bot skeleton and stubs

* Add opsgenie check and set default to pluginv1

* Fix check and set defaults for opsgenie plugin config

* Fill out bot stubs for opsgenie bot

* Add loadOpsgenieConfig to integrations

* Swap to using bearer token for opsgenie plugin

* Fix formating in opsgenie bot

* Reorder imports to fix lint errors

* Add config field to plugins to allow for different recipient logic

Allows bots that use schedules as recipients to determine their own
logic for defaulting etc

* Add notimplemented err for check health for opsgenie bots

* Add check health to opsgenie client and bot

* Rename recipientsAreSchedules to usersAsRecipients

* Add pluginBearertokencredentials checkAndSetdefaults

* Add resolveAnnotations to reqdata and use schedules from that

* Update integrations/access/opsgenie/bot.go

Co-authored-by: Roman Tkachenko <roman@goteleport.com>

* Prevent default schedules being processed if annotations are set

* Remove loadOpsgenieConfig

* Rename opsgneie addr field

* Add more verbose error messages to opsgenie client

* Update api/proto/teleport/legacy/types/types.proto

Co-authored-by: Roman Tkachenko <roman@goteleport.com>

* Update integrations/access/opsgenie/bot.go

Co-authored-by: Roman Tkachenko <roman@goteleport.com>

* Fix error message for recipients field in opsgenie bot

* Change apiEndpoint field name

* Remove check for unused field from opsgenie config

* Remove usersAsRecipients flag

* Reserve addr and change change field number of api_endpoint

* Update integrations/access/opsgenie/client.go

Co-authored-by: Roman Tkachenko <roman@goteleport.com>

* Rename reqAnnotationresponderskey to reqannotationscheduleskey

* Remove unused check and set defaults

* Rename REqAnnotationScheduleskey

* Use types.Labels alias where possible

* Simplify loop in opsgenie bot to satisfy linter

---------

Co-authored-by: Roman Tkachenko <roman@goteleport.com>
2023-05-31 12:40:55 +00:00

99 lines
3 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 common
import (
"context"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
grpcbackoff "google.golang.org/grpc/backoff"
"github.com/gravitational/teleport/api/client"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/integrations/access/common/teleport"
"github.com/gravitational/teleport/integrations/lib"
"github.com/gravitational/teleport/integrations/lib/credentials"
"github.com/gravitational/teleport/integrations/lib/logger"
)
type PluginConfiguration interface {
GetTeleportClient(ctx context.Context) (teleport.Client, error)
GetRecipients() RawRecipientsMap
NewBot(clusterName string, webProxyAddr string) (MessagingBot, error)
GetPluginType() types.PluginType
}
type BaseConfig struct {
Teleport lib.TeleportConfig
Recipients RawRecipientsMap `toml:"role_to_recipients"`
Log logger.Config
PluginType types.PluginType
}
func (c BaseConfig) GetRecipients() RawRecipientsMap {
return c.Recipients
}
func (c BaseConfig) GetTeleportClient(ctx context.Context) (teleport.Client, error) {
if validCred, err := credentials.CheckIfExpired(c.Teleport.Credentials()); err != nil {
log.Warn(err)
if !validCred {
return nil, trace.BadParameter(
"No valid credentials found, this likely means credentials are expired. In this case, please sign new credentials and increase their TTL if needed.",
)
}
log.Info("At least one non-expired credential has been found, continuing startup")
}
bk := grpcbackoff.DefaultConfig
bk.MaxDelay = grpcBackoffMaxDelay
clt, err := client.New(ctx, client.Config{
Addrs: c.Teleport.GetAddrs(),
Credentials: c.Teleport.Credentials(),
DialOpts: []grpc.DialOption{
grpc.WithConnectParams(grpc.ConnectParams{Backoff: bk, MinConnectTimeout: initTimeout}),
grpc.WithDefaultCallOptions(
grpc.WaitForReady(true),
),
grpc.WithReturnConnectionError(),
},
})
if err != nil {
return nil, trace.Wrap(err)
}
return clt, nil
}
// GetPluginType returns the type of plugin this config is for.
func (c BaseConfig) GetPluginType() types.PluginType {
return c.PluginType
}
// GenericAPIConfig holds common configuration use by a messaging service.
// MessagingBots requiring more custom configuration (MSTeams for example) can
// implement their own APIConfig instead.
type GenericAPIConfig struct {
Token string
// DELETE IN 11.0.0 (Joerger) - use "role_to_recipients["*"]" instead
Recipients []string
APIURL string
}