forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: move function getListener() to pkg/net/listener.go
Signed-off-by: YaoZengzeng <[email protected]>
- Loading branch information
1 parent
04b5951
commit 0cb3263
Showing
12 changed files
with
151 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package net | ||
package netutils | ||
|
||
import ( | ||
"bufio" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package net | ||
package netutils | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package netutils | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/alibaba/pouch/pkg/user" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// GetListener get a listener for an address. | ||
func GetListener(addr string, tlsConfig *tls.Config) (net.Listener, error) { | ||
addrParts := strings.SplitN(addr, "://", 2) | ||
if len(addrParts) != 2 { | ||
return nil, fmt.Errorf("invalid listening address %s: must be in format [protocol]://[address]", addr) | ||
} | ||
|
||
switch addrParts[0] { | ||
case "tcp": | ||
l, err := net.Listen("tcp", addrParts[1]) | ||
if err != nil { | ||
return l, err | ||
} | ||
if tlsConfig != nil { | ||
l = tls.NewListener(l, tlsConfig) | ||
} | ||
return l, err | ||
case "unix": | ||
return newUnixSocket(addrParts[1]) | ||
|
||
default: | ||
return nil, fmt.Errorf("only unix socket or tcp address is support") | ||
} | ||
} | ||
|
||
func newUnixSocket(path string) (net.Listener, error) { | ||
if err := syscall.Unlink(path); err != nil && !os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
oldmask := syscall.Umask(0777) | ||
defer syscall.Umask(oldmask) | ||
l, err := net.Listen("unix", path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// chmod unix socket, make other group writable | ||
if err := os.Chmod(path, 0660); err != nil { | ||
l.Close() | ||
return nil, fmt.Errorf("failed to chmod %s: %s", path, err) | ||
} | ||
|
||
gid, err := user.ParseID(user.GroupFile, "pouch", func(line, str string, idInt int, idErr error) (uint32, bool) { | ||
var ( | ||
name, placeholder string | ||
id int | ||
) | ||
|
||
user.ParseString(line, &name, &placeholder, &id) | ||
if str == name { | ||
return uint32(id), true | ||
} | ||
return 0, false | ||
}) | ||
if err != nil { | ||
// ignore error when group pouch not exist, group pouch should to be | ||
// created before pouchd started, it means code not create pouch group | ||
logrus.Warnf("failed to find group pouch, cannot change unix socket %s to pouch group", path) | ||
return l, nil | ||
} | ||
|
||
// chown unix socket with group pouch | ||
if err := os.Chown(path, 0, int(gid)); err != nil { | ||
l.Close() | ||
return nil, fmt.Errorf("failed to chown %s: %s", path, err) | ||
} | ||
return l, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package netutils | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetListenerBasic(t *testing.T) { | ||
type args struct { | ||
addr string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr error | ||
}{ | ||
{ | ||
"tcpAddressTest", | ||
args{"tcp://127.0.0.1:12345"}, | ||
nil, | ||
}, | ||
{ | ||
"unixAddressTest", | ||
args{"unix:///tmp/pouchtest.sock"}, | ||
nil, | ||
}, | ||
{ | ||
"otherProtocolTest", | ||
args{"udp://127.0.0.1:12345"}, | ||
fmt.Errorf("only unix socket or tcp address is support"), | ||
}, | ||
{ | ||
"invalidAddressTest", | ||
args{"invalid address"}, | ||
fmt.Errorf("invalid listening address invalid address: must be in format [protocol]://[address]"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l, err := GetListener(tt.args.addr, nil) | ||
if !reflect.DeepEqual(err, tt.wantErr) { | ||
t.Errorf("GetListener() return error %v, want %v", err, tt.wantErr) | ||
} | ||
if err == nil { | ||
l.Close() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters