-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support AWS sms feature and fix phonenumber verification
Signed-off-by: Bennu-Li <yunmei.li@zilliz.com>
- Loading branch information
Showing
8 changed files
with
214 additions
and
4 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
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
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,92 @@ | ||
package sms | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/sns" | ||
"github.com/kubesphere/notification-manager/pkg/apis/v2beta2" | ||
"github.com/kubesphere/notification-manager/pkg/controller" | ||
"github.com/kubesphere/notification-manager/pkg/utils" | ||
) | ||
|
||
const ( | ||
awsMaxPhoneNums = 1000 | ||
) | ||
|
||
type AWSNotifier struct { | ||
notifierCtl *controller.Controller | ||
AccessKeyId *v2beta2.Credential | ||
SecretAccessKey *v2beta2.Credential | ||
PhoneNums string | ||
Region string | ||
} | ||
|
||
func NewAWSProvider(c *controller.Controller, providers *v2beta2.Providers, phoneNumbers []string) Provider { | ||
phoneNums := handleAWSPhoneNums(phoneNumbers) | ||
return &AWSNotifier{ | ||
notifierCtl: c, | ||
AccessKeyId: providers.AWS.AccessKeyId, | ||
SecretAccessKey: providers.AWS.SecretAccessKey, | ||
PhoneNums: phoneNums, | ||
Region: providers.AWS.Region, | ||
} | ||
} | ||
|
||
type SNSPublishAPI interface { | ||
Publish(ctx context.Context, | ||
params *sns.PublishInput, | ||
optFns ...func(*sns.Options)) (*sns.PublishOutput, error) | ||
} | ||
|
||
func PublishMessage(c context.Context, api SNSPublishAPI, input *sns.PublishInput) (*sns.PublishOutput, error) { | ||
return api.Publish(c, input) | ||
} | ||
|
||
func (a *AWSNotifier) MakeRequest(_ context.Context, messages string) error { | ||
accessKeyId, err := a.notifierCtl.GetCredential(a.AccessKeyId) | ||
if err != nil { | ||
return utils.Errorf("[AWS SendSms] cannot get accessKeyId: %s", err.Error()) | ||
} | ||
secretAccessKey, err := a.notifierCtl.GetCredential(a.SecretAccessKey) | ||
if err != nil { | ||
return utils.Errorf("[AWS SendSms] cannot get secretAccessKey: %s", err.Error()) | ||
} | ||
|
||
err = os.Setenv("AWS_ACCESS_KEY_ID", accessKeyId) | ||
if err != nil { | ||
return utils.Errorf("[AWS SendSms] set environment variables AWS_ACCESS_KEY_ID: %s", err.Error()) | ||
} | ||
err = os.Setenv("AWS_SECRET_ACCESS_KEY", secretAccessKey) | ||
if err != nil { | ||
return utils.Errorf("[AWS SendSms] set environment variables AWS_SECRET_ACCESS_KEY: %s", err.Error()) | ||
} | ||
|
||
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(a.Region)) | ||
if err != nil { | ||
return utils.Errorf("[AWS SendSms]configuration error: %s", err.Error()) | ||
} | ||
|
||
client := sns.NewFromConfig(cfg) | ||
|
||
input := &sns.PublishInput{ | ||
Message: &messages, | ||
PhoneNumber: &a.PhoneNums, | ||
} | ||
|
||
_, err = PublishMessage(context.TODO(), client, input) | ||
if err != nil { | ||
return utils.Errorf("[AWS SendSms] Send failed: %s", err.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func handleAWSPhoneNums(phoneNumbers []string) string { | ||
if len(phoneNumbers) > awsMaxPhoneNums { | ||
phoneNumbers = phoneNumbers[:awsMaxPhoneNums] | ||
} | ||
return strings.Join(phoneNumbers, ",") | ||
} |
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