Restructure packages for reuse

This commit is contained in:
Arthur Barr
2017-11-21 14:32:09 +00:00
parent 3848c39147
commit 099397442b
9 changed files with 49 additions and 66 deletions

View File

@@ -1,158 +0,0 @@
/*
© Copyright IBM Corporation 2017
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// capabilities allows querying of information on Linux capabilities
package capabilities
import (
"errors"
"strconv"
"strings"
)
// DetectCapabilities determines Linux capabilities, based on the contents of a Linux "status" file.
// For example, the contents of file `/proc/1/status`
func DetectCapabilities(status string) ([]string, error) {
lines := strings.Split(status, "\n")
for _, line := range lines {
if strings.HasPrefix(strings.TrimSpace(line), "CapPrm:") {
words := strings.Fields(line)
cap, err := strconv.ParseUint(words[1], 16, 64)
if err != nil {
return nil, err
}
return getCapabilities(cap), nil
}
}
return nil, errors.New("Unable to detect capabilities")
}
// getCapabilities converts an encoded uint64 into a slice of string names of Linux capabilities
func getCapabilities(cap uint64) []string {
caps := make([]string, 0, 37)
if cap&0x0000000040000000 == 0x0000000040000000 {
caps = append(caps, "AUDIT_CONTROL")
}
if cap&0x0000000020000000 == 0x0000000020000000 {
caps = append(caps, "AUDIT_WRITE")
}
if cap&0x0000001000000000 == 0x0000001000000000 {
caps = append(caps, "BLOCK_SUSPEND")
}
if cap&0x0000000000000001 == 0x0000000000000001 {
caps = append(caps, "CHOWN")
}
if cap&0x0000000000000002 == 0x0000000000000002 {
caps = append(caps, "DAC_OVERRIDE")
}
if cap&0x0000000000000004 == 0x0000000000000004 {
caps = append(caps, "DAC_READ_SEARCH")
}
if cap&0x0000000000000008 == 0x0000000000000008 {
caps = append(caps, "FOWNER")
}
if cap&0x0000000000000010 == 0x0000000000000010 {
caps = append(caps, "FSETID")
}
if cap&0x0000000000004000 == 0x0000000000004000 {
caps = append(caps, "IPC_LOCK")
}
if cap&0x0000000000008000 == 0x0000000000008000 {
caps = append(caps, "IPC_OWNER")
}
if cap&0x0000000000000020 == 0x0000000000000020 {
caps = append(caps, "KILL")
}
if cap&0x0000000010000000 == 0x0000000010000000 {
caps = append(caps, "LEASE")
}
if cap&0x0000000000000200 == 0x0000000000000200 {
caps = append(caps, "LINUX_IMMUTABLE")
}
if cap&0x0000000200000000 == 0x0000000200000000 {
caps = append(caps, "MAC_ADMIN")
}
if cap&0x0000000100000000 == 0x0000000100000000 {
caps = append(caps, "MAC_OVERRIDE")
}
if cap&0x0000000008000000 == 0x0000000008000000 {
caps = append(caps, "MKNOD")
}
if cap&0x0000000000001000 == 0x0000000000001000 {
caps = append(caps, "NET_ADMIN")
}
if cap&0x0000000000000400 == 0x0000000000000400 {
caps = append(caps, "NET_BIND_SERVICE")
}
if cap&0x0000000000000800 == 0x0000000000000800 {
caps = append(caps, "NET_BROADCAST")
}
if cap&0x0000000000002000 == 0x0000000000002000 {
caps = append(caps, "NET_RAW")
}
if cap&0x0000000080000000 == 0x0000000080000000 {
caps = append(caps, "SETFCAP")
}
if cap&0x0000000000000040 == 0x0000000000000040 {
caps = append(caps, "SETGID")
}
if cap&0x0000000000000100 == 0x0000000000000100 {
caps = append(caps, "SETPCAP")
}
if cap&0x0000000000000080 == 0x0000000000000080 {
caps = append(caps, "SETUID")
}
if cap&0x0000000400000000 == 0x0000000400000000 {
caps = append(caps, "SYSLOG")
}
if cap&0x0000000000200000 == 0x0000000000200000 {
caps = append(caps, "SYS_ADMIN")
}
if cap&0x0000000000400000 == 0x0000000000400000 {
caps = append(caps, "SYS_BOOT")
}
if cap&0x0000000000040000 == 0x0000000000040000 {
caps = append(caps, "SYS_CHROOT")
}
if cap&0x0000000000010000 == 0x0000000000010000 {
caps = append(caps, "SYS_MODULE")
}
if cap&0x0000000000800000 == 0x0000000000800000 {
caps = append(caps, "SYS_NICE")
}
if cap&0x0000000000100000 == 0x0000000000100000 {
caps = append(caps, "SYS_PACCT")
}
if cap&0x0000000000080000 == 0x0000000000080000 {
caps = append(caps, "SYS_PTRACE")
}
if cap&0x0000000000020000 == 0x0000000000020000 {
caps = append(caps, "SYS_RAWIO")
}
if cap&0x0000000001000000 == 0x0000000001000000 {
caps = append(caps, "SYS_RESOURCE")
}
if cap&0x0000000002000000 == 0x0000000002000000 {
caps = append(caps, "SYS_TIME")
}
if cap&0x0000000004000000 == 0x0000000004000000 {
caps = append(caps, "SYS_TTY_CONFIG")
}
if cap&0x0000000800000000 == 0x0000000800000000 {
caps = append(caps, "WAKE_ALARM")
}
return caps
}

View File

@@ -1,39 +0,0 @@
/*
© Copyright IBM Corporation 2017
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package capabilities
import (
"reflect"
"testing"
)
var capTests = []struct {
in uint64
out []string
}{
{0x0000000040000000, []string{"AUDIT_CONTROL"}},
// Default values when you run a Docker container without changing capabilities:
{0x00000000a80425fb, []string{"AUDIT_WRITE", "CHOWN", "DAC_OVERRIDE", "FOWNER", "FSETID", "KILL", "MKNOD", "NET_BIND_SERVICE", "NET_RAW", "SETFCAP", "SETGID", "SETPCAP", "SETUID", "SYS_CHROOT"}},
}
func TestGetCapabilities(t *testing.T) {
for _, table := range capTests {
caps := getCapabilities(table.in)
if !reflect.DeepEqual(caps, table.out) {
t.Errorf("getCapabilities(%v) - expected %v, got %v", table.in, table.out, caps)
}
}
}

View File

@@ -1,48 +0,0 @@
/*
© Copyright IBM Corporation 2017
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package name contains code to manage the queue manager name
package name
import (
"os"
"regexp"
//log "github.com/sirupsen/logrus"
)
// sanitizeQueueManagerName removes any invalid characters from a queue manager name
// TODO: This is duplicate code
func sanitizeQueueManagerName(name string) string {
var re = regexp.MustCompile("[^a-zA-Z0-9._%/]")
return re.ReplaceAllString(name, "")
}
// GetQueueManagerName resolves the queue manager name to use. Resolved from
// either an environment variable, or the hostname.
func GetQueueManagerName() (string, error) {
var name string
var err error
name, ok := os.LookupEnv("MQ_QMGR_NAME")
if !ok || name == "" {
name, err = os.Hostname()
if err != nil {
return "", err
}
name = sanitizeQueueManagerName(name)
}
// TODO: What if the specified env variable is an invalid name?
return name, nil
}

View File

@@ -1,56 +0,0 @@
/*
© Copyright IBM Corporation 2017
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package name
import (
"os"
"testing"
)
var sanitizeTests = []struct {
in string
out string
}{
{"foo", "foo"},
{"foo-0", "foo0"},
{"foo-", "foo"},
{"-foo", "foo"},
{"foo_0", "foo_0"},
}
func TestSanitizeQueueManagerName(t *testing.T) {
for _, table := range sanitizeTests {
s := sanitizeQueueManagerName(table.in)
if s != table.out {
t.Errorf("sanitizeQueueManagerName(%v) - expected %v, got %v", table.in, table.out, s)
}
}
}
func TestGetQueueManagerNameFromEnv(t *testing.T) {
const data string = "foo"
err := os.Setenv("MQ_QMGR_NAME", data)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
n, err := GetQueueManagerName()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if n != data {
t.Errorf("Expected name=%v, got name=%v", data, n)
}
}