Skip to content

Commit

Permalink
Use string instead of enum
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardjkim committed Oct 29, 2024
1 parent b4f5ab5 commit 2b006eb
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 464 deletions.
23 changes: 1 addition & 22 deletions api/proto/teleport/legacy/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6614,28 +6614,7 @@ message SMTPSpec {
int32 port = 2;
// StartTLSPolicy specifies the SMTP start TLS policy used to send emails over
// SMTP.
SMTPStartTLSPolicy start_tls_policy = 3;
}

// SMTPStartTLSPolicy defines the start TLS policy used to communicate with the
// SMTP service.
enum SMTPStartTLSPolicy {
// SMTP_START_TLS_POLICY_UNSPECIFIED is the default zero value.
SMTP_START_TLS_POLICY_UNSPECIFIED = 0;

// SMTP_START_TLS_POLICY_MANDATORY means that SMTP transactions must be
// encrypted. SMTP transactions are aborted unless STARTTLS is supported by
// the SMTP server. Recommended for all modern SMTP servers.
SMTP_START_TLS_POLICY_MANDATORY = 1;

// SMTP_START_TLS_POLICY_OPPORTUNISTIC means that SMTP transactions are
// encrypted if STARTTLS is supported by the SMTP server. Otherwise, messages
// are sent in the clear.
SMTP_START_TLS_POLICY_OPPORTUNISTIC = 2;

// SMTP_START_TLS_POLICY_DISABLED means encryption is disabled and messages
// are sent in the clear.
SMTP_START_TLS_POLICY_DISABLED = 3;
string start_tls_policy = 3;
}

message PluginBootstrapCredentialsV1 {
Expand Down
4 changes: 2 additions & 2 deletions api/types/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,8 @@ func (c *SMTPSpec) CheckAndSetDefaults() error {
if c.Port == 0 {
return trace.BadParameter("port must be set")
}
if c.StartTlsPolicy == SMTPStartTLSPolicy_SMTP_START_TLS_POLICY_UNSPECIFIED {
c.StartTlsPolicy = SMTPStartTLSPolicy_SMTP_START_TLS_POLICY_MANDATORY
if c.StartTlsPolicy == "" {
return trace.BadParameter("start TLS policy must be set")
}
return nil
}
Expand Down
62 changes: 33 additions & 29 deletions api/types/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,22 @@ func TestPluginEmailSettings(t *testing.T) {
},
}
}
validMailgunSpec := func() *PluginEmailSettings_MailgunSpec {
return &PluginEmailSettings_MailgunSpec{
MailgunSpec: &MailgunSpec{
Domain: "sandbox.mailgun.org",
},
}
}
validSMTPSpec := func() *PluginEmailSettings_SmtpSpec {
return &PluginEmailSettings_SmtpSpec{
SmtpSpec: &SMTPSpec{
Host: "smtp.example.com",
Port: 587,
StartTlsPolicy: "mandatory",
},
}
}

testCases := []struct {
name string
Expand Down Expand Up @@ -1141,11 +1157,7 @@ func TestPluginEmailSettings(t *testing.T) {
{
name: "(mailgun) no credentials",
mutateSettings: func(s *PluginEmailSettings) {
s.Spec = &PluginEmailSettings_MailgunSpec{
MailgunSpec: &MailgunSpec{
Domain: "sandbox.mailgun.org",
},
}
s.Spec = validMailgunSpec()
},
assertErr: func(t require.TestingT, err error, args ...any) {
require.True(t, trace.IsBadParameter(err))
Expand All @@ -1155,11 +1167,7 @@ func TestPluginEmailSettings(t *testing.T) {
{
name: "(mailgun) no credentials label",
mutateSettings: func(s *PluginEmailSettings) {
s.Spec = &PluginEmailSettings_MailgunSpec{
MailgunSpec: &MailgunSpec{
Domain: "sandbox.mailgun.org",
},
}
s.Spec = validMailgunSpec()
},
creds: &PluginCredentialsV1{
Credentials: &PluginCredentialsV1_StaticCredentialsRef{
Expand All @@ -1176,11 +1184,7 @@ func TestPluginEmailSettings(t *testing.T) {
{
name: "(mailgun) valid settings",
mutateSettings: func(s *PluginEmailSettings) {
s.Spec = &PluginEmailSettings_MailgunSpec{
MailgunSpec: &MailgunSpec{
Domain: "sandbox.mailgun.org",
},
}
s.Spec = validMailgunSpec()
},
creds: &PluginCredentialsV1{
Credentials: &PluginCredentialsV1_StaticCredentialsRef{
Expand Down Expand Up @@ -1232,7 +1236,7 @@ func TestPluginEmailSettings(t *testing.T) {
},
},
{
name: "(smtp) no credentials",
name: "(smtp) no start TLS policy",
mutateSettings: func(s *PluginEmailSettings) {
s.Spec = &PluginEmailSettings_SmtpSpec{
SmtpSpec: &SMTPSpec{
Expand All @@ -1241,6 +1245,16 @@ func TestPluginEmailSettings(t *testing.T) {
},
}
},
assertErr: func(t require.TestingT, err error, args ...any) {
require.True(t, trace.IsBadParameter(err))
require.Contains(t, err.Error(), "start TLS policy must be set")
},
},
{
name: "(smtp) no credentials",
mutateSettings: func(s *PluginEmailSettings) {
s.Spec = validSMTPSpec()
},
assertErr: func(t require.TestingT, err error, args ...any) {
require.True(t, trace.IsBadParameter(err))
require.Contains(t, err.Error(), "must be used with the static credentials ref type")
Expand All @@ -1249,12 +1263,7 @@ func TestPluginEmailSettings(t *testing.T) {
{
name: "(smtp) no credentials label",
mutateSettings: func(s *PluginEmailSettings) {
s.Spec = &PluginEmailSettings_SmtpSpec{
SmtpSpec: &SMTPSpec{
Host: "smtp.example.com",
Port: 587,
},
}
s.Spec = validSMTPSpec()
},
creds: &PluginCredentialsV1{
Credentials: &PluginCredentialsV1_StaticCredentialsRef{
Expand All @@ -1271,12 +1280,7 @@ func TestPluginEmailSettings(t *testing.T) {
{
name: "(smtp) valid settings",
mutateSettings: func(s *PluginEmailSettings) {
s.Spec = &PluginEmailSettings_SmtpSpec{
SmtpSpec: &SMTPSpec{
Host: "smtp.example.com",
Port: 587,
},
}
s.Spec = validSMTPSpec()
},
creds: &PluginCredentialsV1{
Credentials: &PluginCredentialsV1_StaticCredentialsRef{
Expand All @@ -1298,7 +1302,7 @@ func TestPluginEmailSettings(t *testing.T) {
SmtpSpec: &SMTPSpec{
Host: "smtp.example.com",
Port: 587,
StartTlsPolicy: SMTPStartTLSPolicy_SMTP_START_TLS_POLICY_OPPORTUNISTIC,
StartTlsPolicy: "opportunistic",
},
}
},
Expand Down
Loading

0 comments on commit 2b006eb

Please sign in to comment.