| 1 | package gandiscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/ |
| 2 | |
| 3 | import ( |
| 4 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 5 | "net/httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/" |
| 6 | "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/" |
| 7 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 9 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 10 | |
| 11 | "github.com/go-acme/lego/v4/platform/testerscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester`/" |
| 12 | "github.com/go-acme/lego/v4/platform/tester/servermockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester/servermock`/" |
| 13 | "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/" |
| 14 | ) |
| 15 | |
| 16 | var envTestscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/envTest. = testerscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester`/.NewEnvTestscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester`/NewEnvTest().(EnvAPIKeyscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/EnvAPIKey.) |
| 17 | |
| 18 | func TestNewDNSProviderscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/TestNewDNSProvider().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 19 | testCases := []struct { |
| 20 | desc string |
| 21 | envVars map[string]string |
| 22 | expected string |
| 23 | }{ |
| 24 | { |
| 25 | desc: "success", |
| 26 | envVars: map[string]string{ |
| 27 | EnvAPIKeyscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/EnvAPIKey.: "123", |
| 28 | }, |
| 29 | }, |
| 30 | { |
| 31 | desc: "missing api key", |
| 32 | envVars: map[string]string{ |
| 33 | EnvAPIKeyscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/EnvAPIKey.: "", |
| 34 | }, |
| 35 | expected: "gandi: some credentials information are missing: GANDI_API_KEY", |
| 36 | }, |
| 37 | } |
| 38 | |
| 39 | for _, test := range testCases { |
| 40 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(test.desc, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 41 | defer envTestscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/envTest..RestoreEnvscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester`/EnvTest#RestoreEnv().() |
| 42 | |
| 43 | envTestscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/envTest..ClearEnvscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester`/EnvTest#ClearEnv().() |
| 44 | |
| 45 | envTestscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/envTest..Applyscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester`/EnvTest#Apply().(test.envVars) |
| 46 | |
| 47 | p, err := NewDNSProviderscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/NewDNSProvider().() |
| 48 | |
| 49 | if test.expected == "" { |
| 50 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 51 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NotNilscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NotNil().(t, p) |
| 52 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NotNilscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NotNil().(t, p.configscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#config.) |
| 53 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NotNilscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NotNil().(t, p.inProgressFQDNsscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#inProgressFQDNs.) |
| 54 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NotNilscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NotNil().(t, p.inProgressAuthZonesscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#inProgressAuthZones.) |
| 55 | } else { |
| 56 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.EqualErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/EqualError().(t, err, test.expected) |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestNewDNSProviderConfigscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/TestNewDNSProviderConfig().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 63 | testCases := []struct { |
| 64 | desc string |
| 65 | apiKey string |
| 66 | expected string |
| 67 | }{ |
| 68 | { |
| 69 | desc: "success", |
| 70 | apiKey: "123", |
| 71 | }, |
| 72 | { |
| 73 | desc: "missing credentials", |
| 74 | expected: "gandi: no API Key given", |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | for _, test := range testCases { |
| 79 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(test.desc, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 80 | config := NewDefaultConfigscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/NewDefaultConfig().() |
| 81 | config.APIKeyscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/Config#APIKey. = test.apiKey |
| 82 | |
| 83 | p, err := NewDNSProviderConfigscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/NewDNSProviderConfig().(config) |
| 84 | |
| 85 | if test.expected == "" { |
| 86 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 87 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NotNilscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NotNil().(t, p) |
| 88 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NotNilscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NotNil().(t, p.configscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#config.) |
| 89 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NotNilscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NotNil().(t, p.inProgressFQDNsscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#inProgressFQDNs.) |
| 90 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NotNilscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NotNil().(t, p.inProgressAuthZonesscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#inProgressAuthZones.) |
| 91 | } else { |
| 92 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.EqualErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/EqualError().(t, err, test.expected) |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // TestDNSProvider runs Present and CleanUp against a fake Gandi RPC |
| 99 | // Server, whose responses are predetermined for particular requests. |
| 100 | func TestDNSProviderscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/TestDNSProvider().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 101 | // serverResponses is the XML-RPC Request->Response map used by the |
| 102 | // fake RPC server. It was generated by recording a real RPC session |
| 103 | // which resulted in the successful issue of a cert, and then |
| 104 | // anonymizing the RPC data. |
| 105 | serverResponses := map[string]string{ |
| 106 | // Present Request->Response 1 (getZoneID) |
| 107 | presentGetZoneIDRequestMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentGetZoneIDRequestMock.: presentGetZoneIDResponseMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentGetZoneIDResponseMock., |
| 108 | // Present Request->Response 2 (cloneZone) |
| 109 | presentCloneZoneRequestMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentCloneZoneRequestMock.: presentCloneZoneResponseMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentCloneZoneResponseMock., |
| 110 | // Present Request->Response 3 (newZoneVersion) |
| 111 | presentNewZoneVersionRequestMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentNewZoneVersionRequestMock.: presentNewZoneVersionResponseMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentNewZoneVersionResponseMock., |
| 112 | // Present Request->Response 4 (addTXTRecord) |
| 113 | presentAddTXTRecordRequestMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentAddTXTRecordRequestMock.: presentAddTXTRecordResponseMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentAddTXTRecordResponseMock., |
| 114 | // Present Request->Response 5 (setZoneVersion) |
| 115 | presentSetZoneVersionRequestMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentSetZoneVersionRequestMock.: presentSetZoneVersionResponseMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentSetZoneVersionResponseMock., |
| 116 | // Present Request->Response 6 (setZone) |
| 117 | presentSetZoneRequestMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentSetZoneRequestMock.: presentSetZoneResponseMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/presentSetZoneResponseMock., |
| 118 | // CleanUp Request->Response 1 (setZone) |
| 119 | cleanupSetZoneRequestMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/cleanupSetZoneRequestMock.: cleanupSetZoneResponseMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/cleanupSetZoneResponseMock., |
| 120 | // CleanUp Request->Response 2 (deleteZone) |
| 121 | cleanupDeleteZoneRequestMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/cleanupDeleteZoneRequestMock.: cleanupDeleteZoneResponseMockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/cleanupDeleteZoneResponseMock., |
| 122 | } |
| 123 | |
| 124 | regexpDate := regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`\[ACME Challenge [^\]:]*:[^\]]*\]`) |
| 125 | |
| 126 | provider := servermockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester/servermock`/.NewBuilderscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester/servermock`/NewBuilder().( |
| 127 | func(server *httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.Serverscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#) (*DNSProviderscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#, error) { |
| 128 | config := NewDefaultConfigscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/NewDefaultConfig().() |
| 129 | config.BaseURLscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/Config#BaseURL. = server.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL. + "/" |
| 130 | config.HTTPClientscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/Config#HTTPClient. = server.Clientscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Client().() |
| 131 | config.APIKeyscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/Config#APIKey. = "123412341234123412341234" |
| 132 | |
| 133 | return NewDNSProviderConfigscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/NewDNSProviderConfig().(config) |
| 134 | }, |
| 135 | servermockscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester/servermock`/.CheckHeaderscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester/servermock`/CheckHeader().().WithContentTypescip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester/servermock`/HeaderLink#WithContentType().("text/xml"), |
| 136 | ). |
| 137 | Routescip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester/servermock`/Builder#Route().("POST /", httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/HandlerFunc#(func(rw httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/ResponseWriter#, req *httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Request#) { |
| 138 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/Equal().(t, "text/xml", req.Headerscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Header#Get().("Content-Type"), "invalid content type") |
| 139 | |
| 140 | body, errS := ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadAllscip-go gomod github.com/golang/go/src go1.25.0 io/ReadAll().(req.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/Request#Body.) |
| 141 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NoError().(t, errS) |
| 142 | |
| 143 | body = regexpDate.ReplaceAllLiteralscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#ReplaceAllLiteral().(body, []byte(`[ACME Challenge 01 Jan 16 00:00 +0000]`)) |
| 144 | resp, ok := serverResponses[string(body)] |
| 145 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.Truefscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/Truef().(t, ok, "Server response for request not found: %s", string(body)) |
| 146 | |
| 147 | _, errS = ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Copyscip-go gomod github.com/golang/go/src go1.25.0 io/Copy().(rw, stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 strings/NewReader().(resp)) |
| 148 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NoError().(t, errS) |
| 149 | })). |
| 150 | Buildscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/platform/tester/servermock`/Builder#Build().(t) |
| 151 | |
| 152 | fakeKeyAuth := "XXXX" |
| 153 | |
| 154 | // define function to override findZoneByFqdn with |
| 155 | fakeFindZoneByFqdn := func(fqdn string) (string, error) { |
| 156 | return "example.com.", nil |
| 157 | } |
| 158 | |
| 159 | // override findZoneByFqdn function |
| 160 | savedFindZoneByFqdn := provider.findZoneByFqdnscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#findZoneByFqdn. |
| 161 | |
| 162 | t.Cleanupscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Cleanup().(func() { |
| 163 | provider.findZoneByFqdnscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#findZoneByFqdn. = savedFindZoneByFqdn |
| 164 | }) |
| 165 | |
| 166 | provider.findZoneByFqdnscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#findZoneByFqdn. = fakeFindZoneByFqdn |
| 167 | |
| 168 | // run Present |
| 169 | err := provider.Presentscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#Present().("abc.def.example.com", "", fakeKeyAuth) |
| 170 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 171 | |
| 172 | // run CleanUp |
| 173 | err = provider.CleanUpscip-go gomod github.com/go-acme/lego/v4 . `github.com/go-acme/lego/v4/providers/dns/gandi`/DNSProvider#CleanUp().("abc.def.example.com", "", fakeKeyAuth) |
| 174 | requirescip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.11.1 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 175 | } |