From 1b315a73814079d1b552669b8cedf38dc94c6688 Mon Sep 17 00:00:00 2001 From: rekby Date: Sun, 27 Oct 2019 17:54:45 +0300 Subject: [PATCH 1/2] Style, typos --- .golangci.yml | 7 ++ internal/cert_manager/cert-state.go | 2 +- internal/cert_manager/manager.go | 3 +- .../manager_semi_integration_test.go | 2 +- internal/cert_manager/manager_test.go | 3 +- internal/domain_checker/config.go | 69 +++++++++++-------- internal/domain_checker/ip_list.go | 16 ++--- internal/domain_checker/ip_list_test.go | 34 ++++----- internal/proxy/http-proxy.go | 2 +- internal/proxy/http-proxy_test.go | 2 + 10 files changed, 79 insertions(+), 61 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 40479b71..1bc926c9 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,6 +3,8 @@ service: run: deadline: 5m issues-exit-code: 1 + skip-files: + - ".*_mock_test.go" linters-settings: lll: @@ -17,9 +19,14 @@ issues: - path: _test\.go linters: - gocyclo + - gocognit - errcheck - dupl - gosec + - funlen + - linters: + - stylecheck + source: "package (cert_manager|domain_checker)" linters: enable-all: true diff --git a/internal/cert_manager/cert-state.go b/internal/cert_manager/cert-state.go index 18abbcca..4c7354e3 100644 --- a/internal/cert_manager/cert-state.go +++ b/internal/cert_manager/cert-state.go @@ -80,7 +80,7 @@ func (s *certState) WaitFinishIssue(ctx context.Context) (cert *tls.Certificate, select { case <-ctx.Done(): err = ctx.Err() - logger.Warn("Certificate issue waiting context cancelled.", zap.Error(err)) + logger.Warn("Certificate issue waiting context canceled.", zap.Error(err)) return nil, err case <-issueContext.Done(): cert, err = s.Cert() diff --git a/internal/cert_manager/manager.go b/internal/cert_manager/manager.go index 449a396c..9241ba6d 100644 --- a/internal/cert_manager/manager.go +++ b/internal/cert_manager/manager.go @@ -37,7 +37,6 @@ const ( ) const domainKeyRSALength = 2048 -const ONLY_ONE_CERT_TMP = true var errHaveNoCert = errors.New("have no certificate for domain") // may return for any internal error @@ -575,7 +574,7 @@ func (m *Manager) isHTTPValidationRequest(r *http.Request) bool { return strings.HasPrefix(r.URL.Path, httpWellKnown) } -func (m *Manager) HandleHttpValidation(w http.ResponseWriter, r *http.Request) bool { +func (m *Manager) HandleHTTPValidation(w http.ResponseWriter, r *http.Request) bool { if !m.isHTTPValidationRequest(r) { return false } diff --git a/internal/cert_manager/manager_semi_integration_test.go b/internal/cert_manager/manager_semi_integration_test.go index ae406b4b..47303315 100644 --- a/internal/cert_manager/manager_semi_integration_test.go +++ b/internal/cert_manager/manager_semi_integration_test.go @@ -63,7 +63,7 @@ func TestManager_GetCertificateHttp01(t *testing.T) { request = request.WithContext(ctx) if manager.isHTTPValidationRequest(request) { logger.Info("Handle validation request", zap.Reflect("request", request)) - manager.HandleHttpValidation(writer, request) + manager.HandleHTTPValidation(writer, request) } else { logger.Warn("Handle non validation request") writer.WriteHeader(http.StatusInternalServerError) diff --git a/internal/cert_manager/manager_test.go b/internal/cert_manager/manager_test.go index 2c898287..5774e56b 100644 --- a/internal/cert_manager/manager_test.go +++ b/internal/cert_manager/manager_test.go @@ -49,10 +49,11 @@ func init() { } func createTestClient(t *testing.T) *acme.Client { - _, err := http.Get(testACMEServer) + resp, err := http.Get(testACMEServer) if err != nil { t.Fatalf("Can't connect to buoulder server: %q", err) } + resp.Body.Close() client := acme.Client{} client.HTTPClient = &http.Client{ diff --git a/internal/domain_checker/config.go b/internal/domain_checker/config.go index f3fcdf7e..a0f2973c 100644 --- a/internal/domain_checker/config.go +++ b/internal/domain_checker/config.go @@ -48,42 +48,19 @@ func (c *Config) CreateDomainChecker(ctx context.Context) (DomainChecker, error) listCheckers = NewAny(listCheckers, NewRegexp(r)) } - var resolver Resolver - if strings.TrimSpace(c.Resolver) == "" { - resolver = net.DefaultResolver - } else { - stringAddresses := strings.Split(c.Resolver, ",") - var resolvers = make([]dns.ResolverInterface, 0, len(stringAddresses)) - for _, addr := range stringAddresses { - addr = strings.TrimSpace(addr) - if addr == "" { - continue - } - tcpAddr, err := net.ResolveTCPAddr("tcp", addr) - if err != nil { - logger.Error("Can't resolve dns server address string", zap.String("addr", addr), zap.Error(err)) - return nil, err - } - if len(tcpAddr.IP) == 0 { - logger.Error("Can't resolve dns server address ip - it is empty.", zap.String("addr", addr)) - return nil, errors.New("empty ip address") - } - if tcpAddr.Port == 0 { - tcpAddr.Port = 53 // default dns port - } - tcpAddrString := tcpAddr.String() - resolvers = append(resolvers, dns.NewResolver(tcpAddrString)) - } - resolver = dns.NewParallel(resolvers...) + resolver, err := c.createResolver(logger) + if err != nil { + log.DebugError(logger, err, "Create resolver") + return nil, err } SetDefaultResolver(resolver) var ipCheckers Any if c.IPSelf { - selfPublicIpList := NewIPList(ctx, CreateGetSelfPublicBinded(net.InterfaceAddrs)) - selfPublicIpList.StartAutoRenew() - ipCheckers = append(ipCheckers, selfPublicIpList) + selfPublicIPList := NewIPList(ctx, CreateGetSelfPublicBinded(net.InterfaceAddrs)) + selfPublicIPList.StartAutoRenew() + ipCheckers = append(ipCheckers, selfPublicIPList) } if c.IPWhiteList != "" { @@ -108,3 +85,35 @@ func (c *Config) CreateDomainChecker(ctx context.Context) (DomainChecker, error) res := NewAll(listCheckers, ipCheckers) return res, nil } + +func (c *Config) createResolver(logger *zap.Logger) (Resolver, error) { + var resolver Resolver + if strings.TrimSpace(c.Resolver) == "" { + resolver = net.DefaultResolver + } else { + stringAddresses := strings.Split(c.Resolver, ",") + var resolvers = make([]dns.ResolverInterface, 0, len(stringAddresses)) + for _, addr := range stringAddresses { + addr = strings.TrimSpace(addr) + if addr == "" { + continue + } + tcpAddr, err := net.ResolveTCPAddr("tcp", addr) + if err != nil { + logger.Error("Can't resolve dns server address string", zap.String("addr", addr), zap.Error(err)) + return nil, err + } + if len(tcpAddr.IP) == 0 { + logger.Error("Can't resolve dns server address ip - it is empty.", zap.String("addr", addr)) + return nil, errors.New("empty ip address") + } + if tcpAddr.Port == 0 { + tcpAddr.Port = 53 // default dns port + } + tcpAddrString := tcpAddr.String() + resolvers = append(resolvers, dns.NewResolver(tcpAddrString)) + } + resolver = dns.NewParallel(resolvers...) + } + return resolver, nil +} diff --git a/internal/domain_checker/ip_list.go b/internal/domain_checker/ip_list.go index 71f98633..f19ffa97 100644 --- a/internal/domain_checker/ip_list.go +++ b/internal/domain_checker/ip_list.go @@ -17,7 +17,7 @@ import ( ) var ( - nonPublicIpNetworks = []net.IPNet{ + nonPublicIPNetworks = []net.IPNet{ // list networks from https://en.wikipedia.org/wiki/Reserved_IP_addresses mustParseNet("0.0.0.0/8"), mustParseNet("10.0.0.0/8"), @@ -110,8 +110,8 @@ func (s *IPList) IsDomainAllowed(ctx context.Context, domain string) (bool, erro hostIP: for _, ip := range ips { - for _, bindedIp := range s.ips { - if ip.IP.Equal(bindedIp) { + for _, bindedIP := range s.ips { + if ip.IP.Equal(bindedIP) { continue hostIP } } @@ -167,7 +167,7 @@ func (s *IPList) updateIPsByTimer() { type InterfacesAddrFunc func() ([]net.Addr, error) -func getBindedIpAddress(ctx context.Context, interfacesAddr InterfacesAddrFunc) []net.IP { +func getBindedIPAddress(ctx context.Context, interfacesAddr InterfacesAddrFunc) []net.IP { logger := zc.L(ctx) binded, err := interfacesAddr() log.DebugDPanic(logger, err, "Get system addresses", zap.Any("addresses", binded)) @@ -190,7 +190,7 @@ func getBindedIpAddress(ctx context.Context, interfacesAddr InterfacesAddrFunc) func filterPublicOnlyIPs(ips []net.IP) []net.IP { var public = make([]net.IP, 0, len(ips)) for _, ip := range ips { - if isPublicIp(ip) { + if isPublicIP(ip) { public = append(public, ip) } } @@ -199,7 +199,7 @@ func filterPublicOnlyIPs(ips []net.IP) []net.IP { func CreateGetSelfPublicBinded(binded InterfacesAddrFunc) AllowedIPAddresses { var f AllowedIPAddresses = func(ctx context.Context) ([]net.IP, error) { - ips := getBindedIpAddress(ctx, binded) + ips := getBindedIPAddress(ctx, binded) ips = filterPublicOnlyIPs(ips) ips = truncatedCopyIPs(ips) return ips, nil @@ -237,12 +237,12 @@ func mustParseNet(s string) net.IPNet { return *ipnet } -func isPublicIp(ip net.IP) bool { +func isPublicIP(ip net.IP) bool { if len(ip) == 0 { return false } - for _, ipNet := range nonPublicIpNetworks { + for _, ipNet := range nonPublicIPNetworks { if ipNet.Contains(ip) { return false } diff --git a/internal/domain_checker/ip_list_test.go b/internal/domain_checker/ip_list_test.go index 7f086d5f..23b1cd35 100644 --- a/internal/domain_checker/ip_list_test.go +++ b/internal/domain_checker/ip_list_test.go @@ -30,22 +30,22 @@ func TestMastParseNet(t *testing.T) { func TestIsPublicIp(t *testing.T) { td := testdeep.NewT(t) - td.True(isPublicIp(net.ParseIP("8.8.8.8"))) - td.True(isPublicIp(net.ParseIP("2a02:6b8:0:1::feed:0ff"))) - td.False(isPublicIp(net.ParseIP(""))) - td.False(isPublicIp(net.ParseIP("127.0.0.1"))) - td.False(isPublicIp(net.ParseIP("169.254.2.3"))) - td.False(isPublicIp(net.ParseIP("192.168.1.1"))) - td.False(isPublicIp(net.ParseIP("10.4.5.6"))) - td.False(isPublicIp(net.ParseIP("172.16.33.2"))) - td.False(isPublicIp(net.ParseIP("::"))) - td.False(isPublicIp(net.ParseIP("::1"))) - td.False(isPublicIp(net.ParseIP("::ffff:​192.168.0.1"))) - td.False(isPublicIp(net.ParseIP("2001:db8::123"))) - td.False(isPublicIp(net.ParseIP("fe80::33"))) - td.False(isPublicIp(net.ParseIP("FC00::4"))) - td.False(isPublicIp(net.ParseIP("ff00::a"))) - td.False(isPublicIp(net.ParseIP("FF02:0:0:0:0:1:FF00::441"))) + td.True(isPublicIP(net.ParseIP("8.8.8.8"))) + td.True(isPublicIP(net.ParseIP("2a02:6b8:0:1::feed:0ff"))) + td.False(isPublicIP(net.ParseIP(""))) + td.False(isPublicIP(net.ParseIP("127.0.0.1"))) + td.False(isPublicIP(net.ParseIP("169.254.2.3"))) + td.False(isPublicIP(net.ParseIP("192.168.1.1"))) + td.False(isPublicIP(net.ParseIP("10.4.5.6"))) + td.False(isPublicIP(net.ParseIP("172.16.33.2"))) + td.False(isPublicIP(net.ParseIP("::"))) + td.False(isPublicIP(net.ParseIP("::1"))) + td.False(isPublicIP(net.ParseIP("::ffff:192.168.0.1"))) + td.False(isPublicIP(net.ParseIP("2001:db8::123"))) + td.False(isPublicIP(net.ParseIP("fe80::33"))) + td.False(isPublicIP(net.ParseIP("FC00::4"))) + td.False(isPublicIP(net.ParseIP("ff00::a"))) + td.False(isPublicIP(net.ParseIP("FF02:0:0:0:0:1:FF00::441"))) } func TestGetBindedIpAddress(t *testing.T) { @@ -65,7 +65,7 @@ func TestGetBindedIpAddress(t *testing.T) { }, nil } - res := getBindedIpAddress(ctx, f) + res := getBindedIPAddress(ctx, f) td.CmpDeeply(res, []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("161.32.6.19"), net.ParseIP("::1"), net.ParseIP("1.2.3.4"), net.ParseIP("2a02:6b8::feed:0ff")}) diff --git a/internal/proxy/http-proxy.go b/internal/proxy/http-proxy.go index eba296e7..04ca5728 100644 --- a/internal/proxy/http-proxy.go +++ b/internal/proxy/http-proxy.go @@ -71,7 +71,7 @@ func (p *HTTPProxy) Start() error { go func() { <-p.ctx.Done() err := httpServer.Close() - log.DebugErrorCtx(p.ctx, err, "Http builtin reverse proxy stop because context cancelled") + log.DebugErrorCtx(p.ctx, err, "Http builtin reverse proxy stop because context canceled") }() zc.L(p.ctx).Info("Http builtin reverse proxy start") diff --git a/internal/proxy/http-proxy_test.go b/internal/proxy/http-proxy_test.go index eba817a6..7520e12f 100644 --- a/internal/proxy/http-proxy_test.go +++ b/internal/proxy/http-proxy_test.go @@ -124,6 +124,7 @@ func TestNewHttpProxy(t *testing.T) { td.CmpNoError(err) td.CmpDeeply(http.StatusOK, resp.StatusCode) res, err = ioutil.ReadAll(resp.Body) + _ = resp.Body.Close() td.CmpNoError(err) td.CmpDeeply(res, []byte{1, 2, 3}) @@ -131,6 +132,7 @@ func TestNewHttpProxy(t *testing.T) { td.CmpNoError(err) td.CmpDeeply(http.StatusAccepted, resp.StatusCode) res, err = ioutil.ReadAll(resp.Body) + _ = resp.Body.Close() td.CmpNoError(err) td.CmpDeeply(res, []byte{3, 4}) } From c5b178dade468608838a98e5bca43ca6228f382e Mon Sep 17 00:00:00 2001 From: rekby Date: Sun, 27 Oct 2019 17:56:02 +0300 Subject: [PATCH 2/2] revert go version to 1.12 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cc1192bc..100bcc19 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ addons: - dos2unix go: - - "1.10" + - "1.12" services: - docker