Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix acme account deletion without provider change #3664

Merged
merged 8 commits into from
Jul 31, 2018
Merged

Fix acme account deletion without provider change #3664

merged 8 commits into from
Jul 31, 2018

Conversation

zyclonite
Copy link
Contributor

What does this PR do?

prevent traefik from deleting the acme.account without provider change
fixes issue #3650

@traefiker traefiker added this to the 1.7 milestone Jul 24, 2018
@mmatur mmatur changed the title fix acme.account deletion without provider change Fix acme account deletion without provider change Jul 24, 2018
Copy link
Collaborator

@SantoDE SantoDE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👼

Good catch. Well done!

Copy link
Member

@mmatur mmatur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution. Few remarks.
Could you also add some tests. You could maybe extract this new things into a function to be able to test more easily

if p.account != nil && p.account.Registration != nil {
aru, err := url.Parse(p.account.Registration.URI)
if err != nil {
log.Error("Unable to parse account.Registration URL")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to return an error

}
cau, err := url.Parse(p.CAServer)
if err != nil {
log.Error("Unable to parse CAServer URL")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to return an error

acme/acme.go Outdated
if account != nil && account.Registration != nil {
aru, err := url.Parse(account.Registration.URI)
if err != nil {
log.Error("Unable to parse account.Registration URL")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to return an error

acme/acme.go Outdated
}
cau, err := url.Parse(a.CAServer)
if err != nil {
log.Error("Unable to parse CAServer URL")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to return an error

log.Error("Unable to parse CAServer URL")
}
if strings.Compare(cau.Hostname(), aru.Hostname()) != 0 {
log.Error("Account does not match the current CAServer")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change the level of this log and improve the message with something like that

log.Info("Account URI does not match the current CAServer. The account will be reset")

log.Error("Unable to parse CAServer URL")
}
if strings.Compare(cau.Hostname(), aru.Hostname()) != 0 {
log.Error("Account does not match the current CAServer")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change the level of this log and improve the message with something like that

log.Info("Account URI does not match the current CAServer. The account will be reset")

acme/acme.go Outdated
log.Error("Unable to parse CAServer URL")
}
if strings.Compare(cau.Hostname(), aru.Hostname()) != 0 {
log.Error("Account does not match the current CAServer")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change the level of this log and improve the message with something like that

log.Info("Account URI does not match the current CAServer. The account will be reset")

acme/acme.go Outdated
log.Error("Unable to parse CAServer URL")
}
if strings.Compare(cau.Hostname(), aru.Hostname()) != 0 {
log.Error("Account does not match the current CAServer")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change the level of this log and improve the message with something like that

log.Info("Account URI does not match the current CAServer. The account will be reset")

if err != nil {
log.Error("Unable to parse CAServer URL")
}
if strings.Compare(cau.Hostname(), aru.Hostname()) != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you don't return error before - cau and aru can be nil

acme/acme.go Outdated
if err != nil {
log.Error("Unable to parse CAServer URL")
}
if strings.Compare(cau.Hostname(), aru.Hostname()) != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you don't return error before - cau and aru can be nil

@zyclonite
Copy link
Contributor Author

i have no proper go dev environment, it's a bit trail and error with simply a text editor...

as i do not know how to properly write tests in go, it would be nice if someone could support me here

Copy link
Member

@mmatur mmatur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix.

You can start to add a function in provider/acme/provider_test.go

func TestIsAccountMatchingCaServer(t *testing.T) {
	testCases := []struct {
		desc       string
		accountURI string
		serverURI  string
		expected   bool
	}{
		{
			desc:       "description",
			accountURI: "http://powpow.com/",
			serverURI:  "http://powpow2.com/",
			expected:   false,
		},
		{
			desc:       "description 2",
			accountURI: "http://powpow.com/",
			serverURI:  "http://powpow.com/",
			expected:   true,
		},
	}

	for _, test := range testCases {
		test := test
		t.Run(test.desc, func(t *testing.T) {
			t.Parallel()

			result := isAccountMatchingCaServer(test.accountURI, test.serverURI)

			assert.Equal(t, test.expected, result)
		})
	}
}

acme/acme.go Outdated
log.Infof("Unable to parse CAServer URL : %v", err)
return false
}
return strings.Compare(cau.Hostname(), aru.Hostname()) != 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should replace by

return cau.Hostname() == aru.Hostname()

log.Infof("Unable to parse CAServer URL : %v", err)
return false
}
return strings.Compare(cau.Hostname(), aru.Hostname()) != 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should replace by

return cau.Hostname() == aru.Hostname()

acme/acme.go Outdated
@@ -183,7 +184,8 @@ func (a *ACME) leadershipListener(elected bool) error {
account := object.(*Account)
account.Init()
// Reset Account values if caServer changed, thus registration URI can be updated
if account != nil && account.Registration != nil && !strings.HasPrefix(account.Registration.URI, a.CAServer) {
if account != nil && account.Registration != nil && isAccountMatchingCaServer(account.Registration.URI, a.CAServer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's should be !isAccountMatchingCaServer(account.Registration.URI, a.CAServer) instead of isAccountMatchingCaServer(account.Registration.URI, a.CAServer) no ?

@@ -130,7 +131,8 @@ func (p *Provider) Init(_ types.Constraints) error {
}

// Reset Account if caServer changed, thus registration URI can be updated
if p.account != nil && p.account.Registration != nil && !strings.HasPrefix(p.account.Registration.URI, p.CAServer) {
if p.account != nil && p.account.Registration != nil && isAccountMatchingCaServer(p.account.Registration.URI, p.CAServer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's should be !isAccountMatchingCaServer(account.Registration.URI, a.CAServer) instead of isAccountMatchingCaServer(account.Registration.URI, a.CAServer) no ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, my mistake

expected: false,
},
{
desc: "malformed server url",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test with 2 malformed url
Like that:

{
    desc:       "malformed account & server url",
    accountURI: "/test.example.com/acme/acct/1234567",
    serverURI:  "/test.example.com/acme/acct/1234567",
    expected:   false,
},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

problem is, it's not really malformed for the parser.. i'll find a different approach

Copy link
Member

@mmatur mmatur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Great jobs @zyclonite 👏

Copy link
Contributor

@nmengin nmengin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants