Skip to content

Commit

Permalink
Style, revert go to 1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby authored Oct 27, 2019
2 parents 1b761fc + c5b178d commit 98daa1b
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 62 deletions.
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ service:
run:
deadline: 5m
issues-exit-code: 1
skip-files:
- ".*_mock_test.go"

linters-settings:
lll:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ addons:
- dos2unix

go:
- "1.10"
- "1.12"

services:
- docker
Expand Down
2 changes: 1 addition & 1 deletion internal/cert_manager/cert-state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions internal/cert_manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cert_manager/manager_semi_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion internal/cert_manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
69 changes: 39 additions & 30 deletions internal/domain_checker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
}
16 changes: 8 additions & 8 deletions internal/domain_checker/ip_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
34 changes: 17 additions & 17 deletions internal/domain_checker/ip_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")})
Expand Down
2 changes: 1 addition & 1 deletion internal/proxy/http-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions internal/proxy/http-proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ 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})

resp, err = http.Get(prefix + "/asdfg")
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})
}

0 comments on commit 98daa1b

Please sign in to comment.