aws

package
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2023 License: Apache-2.0 Imports: 37 Imported by: 1

Documentation

Overview

Package aws provides functionality for webhook management using AWS. This package will not work with go 1.18+ due to its dependency on SHA1. For further information, please see https://github.com/golang/go/issues/41682.

Deprecated: xmetrics is no longer planned to be used by future WebPA/XMiDT services.

This package is frozen and no new functionality will be added.

Index

Constants

View Source
const (
	DnsReadyQueryCount      = "dns_ready_query_count"
	DnsReady                = "dns_ready"
	SNSNotificationReceived = "webhook_sns_notification_received_count"
	SNSNotificationSent     = "webhook_sns_notification_sent_count"
	SNSSubscribeAttempt     = "webhook_sns_subscribe_attempt_count"
	SNSSubscribed           = "webhook_sns_subscribed_value"
)
View Source
const (
	MSG_ATTR           = "scytale.env"
	SNS_VALIDATION_ERR = "SNS signature validation error"
)
View Source
const (
	TEST_AWS_CONFIG = `` /* 338-byte string literal not displayed */

	TEST_SUB_MSG = `` /* 1317-byte string literal not displayed */

	TEST_HOOK = `` /* 224-byte string literal not displayed */

	NOTIFY_HOOK_MSG = `` /* 1017-byte string literal not displayed */

	TEST_NOTIF_ERR_MSG = `` /* 816-byte string literal not displayed */

)
View Source
const (
	// AWSKey is the subkey used to load AWS configuration (e.g. AWSConfig)
	AWSKey = "aws"
)

Variables

View Source
var (
	ErrJsonEmpty = errors.New("JSON payload is empty")
)

Functions

func DecodeJSONMessage

func DecodeJSONMessage(req *http.Request, v interface{}) ([]byte, error)

func Metrics

func Metrics() []xmetrics.Metric

Metrics returns the defined metrics as a list

func ProvideMetrics

func ProvideMetrics() fx.Option

func SetUpTestNotifier

func SetUpTestNotifier() (Notifier, *MockSVC, *MockValidator, *mux.Router)

Types

type AWSConfig

type AWSConfig struct {
	AccessKey string    `json:"accessKey"`
	SecretKey string    `json:"secretKey"`
	Env       string    `json:"env"`
	Sns       SNSConfig `json:"sns"`
}

func NewAWSConfig

func NewAWSConfig(v *viper.Viper) (c *AWSConfig, err error)

NewAWSConfig produces AWSConfig from Viper environment

type AWSMetrics

type AWSMetrics struct {
	fx.In

	DnsReadyQueryCount      metrics.Counter `name:"dns_ready_query_count"`
	DnsReady                metrics.Gauge   `name:"dns_ready"`
	SNSNotificationReceived metrics.Counter `name:"webhook_sns_notification_received_count"`
	SNSNotificationSent     metrics.Counter `name:"webhook_sns_notification_sent_count"`
	SNSSubscribeAttempt     metrics.Counter `name:"webhook_sns_subscribe_attempt_count"`
	SNSSubscribed           metrics.Gauge   `name:"webhook_sns_subscribed_value"`
}

func ApplyMetricsData

func ApplyMetricsData(registry xmetrics.Registry) (m AWSMetrics)

ApplyMetricsData is used for setting the counter values on the AWSMetrics when stored and accessing for later use

type ErrResp

type ErrResp struct {
	Code    int
	Message string
}

type MockSVC

type MockSVC struct {
	snsiface.SNSAPI
	mock.Mock
}

func (*MockSVC) ConfirmSubscription

func (m *MockSVC) ConfirmSubscription(input *sns.ConfirmSubscriptionInput) (*sns.ConfirmSubscriptionOutput, error)

func (*MockSVC) ListSubscriptionsByTopic

func (m *MockSVC) ListSubscriptionsByTopic(input *sns.ListSubscriptionsByTopicInput) (*sns.ListSubscriptionsByTopicOutput, error)

func (*MockSVC) Publish

func (m *MockSVC) Publish(input *sns.PublishInput) (*sns.PublishOutput, error)

func (*MockSVC) Subscribe

func (m *MockSVC) Subscribe(input *sns.SubscribeInput) (*sns.SubscribeOutput, error)

func (*MockSVC) Unsubscribe

func (m *MockSVC) Unsubscribe(input *sns.UnsubscribeInput) (*sns.UnsubscribeOutput, error)

type MockValidator

type MockValidator struct {
	mock.Mock
}

func (*MockValidator) Validate

func (m *MockValidator) Validate(msg *SNSMessage) (bool, error)

type MsgAttr

type MsgAttr struct {
	Type  string
	Value string
}
http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html

POST / HTTP/1.1 x-amz-sns-message-type: SubscriptionConfirmation x-amz-sns-message-id: 165545c9-2a5c-472c-8df2-7ff2be2b3b1b x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic Content-Length: 1336 Content-Type: text/plain; charset=UTF-8 Host: example.com Connection: Keep-Alive User-Agent: Amazon Simple Notification Service Agent

{
  "Type" : "SubscriptionConfirmation",
  "MessageId" : "165545c9-2a5c-472c-8df2-7ff2be2b3b1b",
  "Token" : "2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736",
  "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
  "Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
  "SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736",
  "Timestamp" : "2012-04-26T20:45:04.751Z",
  "SignatureVersion" : "1",
  "Signature" : "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=",
  "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem"
  }

POST / HTTP/1.1 x-amz-sns-message-type: Notification x-amz-sns-message-id: 22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324 x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:c9135db0-26c4-47ec-8998-413945fb5a96 Content-Length: 773 Content-Type: text/plain; charset=UTF-8 Host: example.com Connection: Keep-Alive User-Agent: Amazon Simple Notification Service Agent

{
  "Type" : "Notification",
  "MessageId" : "22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324",
  "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
  "Subject" : "My First Message",
  "Message" : "Hello world!",
  "Timestamp" : "2012-05-02T00:54:06.655Z",
  "SignatureVersion" : "1",
  "Signature" : "EXAMPLEw6JRNwm1LFQL4ICB0bnXrdB8ClRMTQFGBqwLpGbM78tJ4etTwC5zU7O3tS6tGpey3ejedNdOJ+1fkIp9F2/LmNVKb5aFlYq+9rk9ZiPph5YlLmWsDcyC5T+Sy9/umic5S0UQc2PEtgdpVBahwNOdMW4JPwk0kAJJztnc=",
  "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",
  "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:c9135db0-26c4-47ec-8998-413945fb5a96"
  }

type Notifier

type Notifier interface {
	Initialize(*mux.Router, *url.URL, string, http.Handler, *zap.Logger, AWSMetrics, func() time.Time)
	PrepareAndStart()
	Subscribe()
	PublishMessage(string) error
	Unsubscribe(string)
	NotificationHandle(http.ResponseWriter, *http.Request) []byte
	ValidateSubscriptionArn(string) bool
	SNSNotificationReceivedCounter(int)
	DnsReady() error
}

Notifier interface implements the various notification server functionalities like Subscribe, Unsubscribe, Publish, NotificationHandler

func NewNotifier

func NewNotifier(v *viper.Viper) (Notifier, error)

NewNotifier creates Notifier instance using the viper config

type SNSConfig

type SNSConfig struct {
	Protocol    string `json:"protocol"`
	Region      string `json:"region"`
	TopicArn    string `json:"topicArn"`
	UrlPath     string `json:"urlPath"` //uri path to register mux
	AwsEndpoint string `json:"awsEndpoint"`
}

type SNSMessage

type SNSMessage struct {
	Type              string
	MessageId         string
	Token             string
	TopicArn          string
	Subject           string
	Message           string
	SubscribeURL      string
	Timestamp         string
	SignatureVersion  string
	Signature         string
	SigningCertURL    string
	UnsubscribeURL    string
	MessageAttributes map[string]MsgAttr
}

type SNSServer

type SNSServer struct {
	Config AWSConfig

	SVC         snsiface.SNSAPI
	SelfUrl     *url.URL
	SOAProvider string
	SNSValidator
	// contains filtered or unexported fields
}

func NewSNSServer

func NewSNSServer(v *viper.Viper) (ss *SNSServer, err error)

NewSNSServer creates SNSServer instance using viper config

func (*SNSServer) DnsReady

func (ss *SNSServer) DnsReady() (e error)

DnsReady blocks until the primary server's DNS is up and running or until the timeout is reached if timeout value is 0s it will try forever

func (*SNSServer) Initialize

func (ss *SNSServer) Initialize(rtr *mux.Router, selfUrl *url.URL, soaProvider string,
	handler http.Handler, logger *zap.Logger, metrics AWSMetrics, now func() time.Time)

Initialize initializes the SNSServer fields selfURL represents the webhook server URL &url.URL{Scheme:secure,Host:fqdn+port,Path:urlPath} handler is the webhook handler to update webhooks @monitor SNS POST Notification handler will directly update webhooks list

func (*SNSServer) ListSubscriptionsByMatchingEndpoint

func (ss *SNSServer) ListSubscriptionsByMatchingEndpoint() (*list.List, error)

func (*SNSServer) NotificationHandle

func (ss *SNSServer) NotificationHandle(rw http.ResponseWriter, req *http.Request) []byte

Decodes SNS Notification message and returns the actual message which is json webhook content

func (*SNSServer) PrepareAndStart

func (ss *SNSServer) PrepareAndStart()

Prepare the SNSServer to receive Notifications This better be called after the endpoint http server is started and ready to receive AWS SNS POST messages subscribe to the SNS topic

func (*SNSServer) PublishMessage

func (ss *SNSServer) PublishMessage(message string) error

Publish Notification message to AWS SNS topic

func (*SNSServer) SNSNotificationReceivedCounter

func (ss *SNSServer) SNSNotificationReceivedCounter(code int)

SNSNotificationReceivedCounter relays response code data to be aggregated in metrics

func (*SNSServer) SNSNotificationReceivedInit

func (ss *SNSServer) SNSNotificationReceivedInit() chan int

SNSNotificationReceivedInit initializes metrics counters and returns a channel to send response codes to count

func (*SNSServer) SetSNSRoutes

func (ss *SNSServer) SetSNSRoutes(urlPath string, r *mux.Router, handler http.Handler)

Define handlers for various AWS SNS POST calls

func (*SNSServer) Subscribe

func (ss *SNSServer) Subscribe()

Subscribe to AWS SNS Topic to receive notifications

func (*SNSServer) SubscribeConfirmHandle

func (ss *SNSServer) SubscribeConfirmHandle(rw http.ResponseWriter, req *http.Request)

POST handler to receive SNS Confirmation Message

func (*SNSServer) Unsubscribe

func (ss *SNSServer) Unsubscribe(subArn string)

Unsubscribe from receiving notifications

func (*SNSServer) UnsubscribeOldSubscriptions

func (ss *SNSServer) UnsubscribeOldSubscriptions()

func (*SNSServer) ValidateSubscriptionArn

func (ss *SNSServer) ValidateSubscriptionArn(reqSubscriptionArn string) bool

Validate that SubscriptionArn received in AWS request matches the cached config data

type SNSValidator

type SNSValidator interface {
	Validate(*SNSMessage) (bool, error)
}

func NewSNSValidator

func NewSNSValidator() SNSValidator

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

func NewValidator

func NewValidator(client *http.Client) *Validator

func (*Validator) Validate

func (v *Validator) Validate(msg *SNSMessage) (ok bool, err error)

Validator validates an Amazon SNS message signature. NOTE: This will not work with go 1.18+, which no longer allows SHA1.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL