first commit

This commit is contained in:
2024-10-28 23:04:48 +01:00
commit 1ee55157f1
911 changed files with 325331 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
/*
© Copyright IBM Corporation 2017, 2023
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 containerruntimelogger
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/ibm-messaging/mq-container/internal/containerruntime"
"github.com/ibm-messaging/mq-container/internal/user"
"github.com/ibm-messaging/mq-container/pkg/logger"
)
// LogContainerDetails logs details about the container runtime
func LogContainerDetails(log *logger.Logger) error {
if runtime.GOOS != "linux" {
return fmt.Errorf("Unsupported platform: %v", runtime.GOOS)
}
log.Printf("CPU architecture: %v", runtime.GOARCH)
kv, err := containerruntime.GetKernelVersion()
if err == nil {
log.Printf("Linux kernel version: %v", kv)
}
bi, err := containerruntime.GetBaseImage()
if err == nil {
log.Printf("Base image: %v", bi)
}
u, err := user.GetUser()
if err != nil {
log.Printf("Error: %v\nUser:\n uid: %v\n gid: %v\n supGid: %v", err, u.UID, u.PrimaryGID, u.SupplementalGID)
}
if err == nil {
if len(u.SupplementalGID) == 0 {
log.Printf("Running as user ID %v with primary group %v", u.UID, u.PrimaryGID)
} else {
log.Printf("Running as user ID %v with primary group %v, and supplementary groups %v", u.UID, u.PrimaryGID, strings.Trim(strings.Join(strings.Fields(fmt.Sprint(u.SupplementalGID)), ","), "[]"))
}
}
caps, err := containerruntime.GetCapabilities(1)
capLogged := false
if err == nil {
for k, v := range caps {
if len(v) > 0 {
log.Printf("Capabilities (%s set): %v", strings.ToLower(k), strings.Join(v, ","))
capLogged = true
}
}
if !capLogged {
log.Print("Capabilities: none")
}
} else {
log.Errorf("Error getting capabilities: %v", err)
}
sc := containerruntime.GetSeccomp()
log.Printf("seccomp enforcing mode: %v", sc)
log.Printf("Process security attributes: %v", containerruntime.GetSecurityAttributes())
m, err := containerruntime.GetMounts()
if err == nil {
if len(m) == 0 {
log.Print("No volume detected. Persistent messages may be lost")
} else {
for mountPoint, fsType := range m {
log.Printf("Detected '%v' volume mounted to %v", fsType, mountPoint)
if !containerruntime.SupportedFilesystem(fsType) {
return fmt.Errorf("%v uses unsupported filesystem type: %v", mountPoint, fsType)
}
}
}
}
// For a multi-instance queue manager - check all required mounts exist & validate filesystem type
if os.Getenv("MQ_MULTI_INSTANCE") == "true" {
log.Println("Multi-instance queue manager: enabled")
reqMounts := []string{"/mnt/mqm", "/mnt/mqm-log", "/mnt/mqm-data"}
for _, mountPoint := range reqMounts {
if fsType, ok := m[mountPoint]; ok {
if !containerruntime.ValidMultiInstanceFilesystem(fsType) {
return fmt.Errorf("%v uses filesystem type '%v' which is invalid for a multi-instance queue manager", mountPoint, fsType)
}
} else {
return fmt.Errorf("Missing required mount '%v' for a multi-instance queue manager", mountPoint)
}
}
}
if os.Getenv("MQ_LOGGING_CONSOLE_FORMAT") == "" && os.Getenv("LOG_FORMAT") != "" {
log.Println("Environment variable LOG_FORMAT is deprecated. Use MQ_LOGGING_CONSOLE_FORMAT instead.")
}
return nil
}

173
pkg/logger/logger.go Normal file
View File

@@ -0,0 +1,173 @@
/*
© Copyright IBM Corporation 2018, 2021
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 logger provides utility functions for logging purposes
package logger
import (
"encoding/json"
"fmt"
"io"
"os"
"os/user"
"strconv"
"sync"
"time"
)
// timestampFormat matches the format used by MQ messages (includes milliseconds)
const timestampFormat string = "2006-01-02T15:04:05.000Z07:00"
const debugLevel string = "DEBUG"
const infoLevel string = "INFO"
const errorLevel string = "ERROR"
// A Logger is used to log messages to stdout
type Logger struct {
mutex sync.Mutex
writer io.Writer
debug bool
json bool
processName string
pid string
serverName string
host string
userName string
}
// NewLogger creates a new logger
func NewLogger(writer io.Writer, debug bool, json bool, serverName string) (*Logger, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
// This can fail because the container's running as a random UID which
// is not known by the OS. We don't want this to break the logging
// entirely, so just use a blank user name.
user, err := user.Current()
userName := ""
if err == nil {
userName = user.Username
}
return &Logger{
mutex: sync.Mutex{},
writer: writer,
debug: debug,
json: json,
processName: os.Args[0],
pid: strconv.Itoa(os.Getpid()),
serverName: serverName,
host: hostname,
userName: userName,
}, nil
}
func (l *Logger) format(entry map[string]interface{}) (string, error) {
if l.json {
b, err := json.Marshal(entry)
if err != nil {
return "", err
}
return string(b), err
}
return fmt.Sprintf("%v %v\n", entry["ibm_datetime"], entry["message"]), nil
}
// log logs a message at the specified level. The message is enriched with
// additional fields.
func (l *Logger) log(level string, msg string) {
t := time.Now()
entry := map[string]interface{}{
"message": fmt.Sprint(msg),
"ibm_datetime": t.Format(timestampFormat),
"loglevel": level,
"host": l.host,
"ibm_serverName": l.serverName,
"ibm_processName": l.processName,
"ibm_processId": l.pid,
"ibm_userName": l.userName,
"type": "mq_containerlog",
}
s, err := l.format(entry)
l.mutex.Lock()
if err != nil {
// TODO: Fix this
fmt.Println(err)
}
if l.json {
fmt.Fprintln(l.writer, s)
} else {
fmt.Fprint(l.writer, s)
}
l.mutex.Unlock()
}
// Debug logs a line as debug
func (l *Logger) Debug(args ...interface{}) {
if l.debug {
if l.json {
l.log(debugLevel, fmt.Sprint(args...))
} else {
l.log(debugLevel, "DEBUG: "+fmt.Sprint(args...))
}
}
}
// Debugf logs a line as debug using format specifiers
func (l *Logger) Debugf(format string, args ...interface{}) {
if l.debug {
if l.json {
l.log(debugLevel, fmt.Sprintf(format, args...))
} else {
l.log(debugLevel, fmt.Sprintf("DEBUG: "+format, args...))
}
}
}
// Print logs a message as info
func (l *Logger) Print(args ...interface{}) {
l.log(infoLevel, fmt.Sprint(args...))
}
// Println logs a message
func (l *Logger) Println(args ...interface{}) {
l.Print(args...)
}
// Printf logs a message as info using format specifiers
func (l *Logger) Printf(format string, args ...interface{}) {
l.log(infoLevel, fmt.Sprintf(format, args...))
}
// PrintString logs a string as info
func (l *Logger) PrintString(msg string) {
l.log(infoLevel, msg)
}
// Errorf logs a message as error
func (l *Logger) Error(args ...interface{}) {
l.log(errorLevel, fmt.Sprint(args...))
}
// Errorf logs a message as error using format specifiers
func (l *Logger) Errorf(format string, args ...interface{}) {
l.log(errorLevel, fmt.Sprintf(format, args...))
}
// Fatalf logs a message as fatal using format specifiers
// TODO: Remove this
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.log("FATAL", fmt.Sprintf(format, args...))
}

55
pkg/logger/logger_test.go Normal file
View File

@@ -0,0 +1,55 @@
/*
© Copyright IBM Corporation 2018, 2019
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 logger
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
func TestJSONLogger(t *testing.T) {
buf := new(bytes.Buffer)
l, err := NewLogger(buf, true, true, t.Name())
if err != nil {
t.Fatal(err)
}
s := "Hello world"
l.Print(s)
var e map[string]interface{}
err = json.Unmarshal([]byte(buf.String()), &e)
if err != nil {
t.Error(err)
}
if s != e["message"] {
t.Errorf("Expected JSON to contain message=%v; got %v", s, buf.String())
}
}
func TestSimpleLogger(t *testing.T) {
buf := new(bytes.Buffer)
l, err := NewLogger(buf, true, false, t.Name())
if err != nil {
t.Fatal(err)
}
s := "Hello world"
l.Print(s)
if !strings.Contains(buf.String(), s) {
t.Errorf("Expected log output to contain %v; got %v", s, buf.String())
}
}

5
pkg/mqini/dspmqinf1.txt Normal file
View File

@@ -0,0 +1,5 @@
QueueManager:
Name=foo
Directory=foo
Prefix=/var/mqm
InstallationName=Installation1

5
pkg/mqini/dspmqinf2.txt Normal file
View File

@@ -0,0 +1,5 @@
QueueManager:
Name=a/b
Directory=a&b
Prefix=/var/mqm
InstallationName=Installation1

5
pkg/mqini/dspmqinf3.txt Normal file
View File

@@ -0,0 +1,5 @@
QueueManager:
Name=..
Directory=!!
Prefix=/var/mqm
InstallationName=Installation1

92
pkg/mqini/mqini.go Normal file
View File

@@ -0,0 +1,92 @@
/*
© Copyright IBM Corporation 2018, 2023
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 mqini provides information about queue managers
package mqini
import (
"bufio"
"errors"
"os"
"strings"
"github.com/ibm-messaging/mq-container/internal/command"
"github.com/ibm-messaging/mq-container/internal/pathutils"
)
// QueueManager describe high-level configuration information for a queue manager
type QueueManager struct {
Name string
Prefix string
Directory string
DataPath string
InstallationName string
}
// getQueueManagerFromStanza parses a queue manager stanza
func getQueueManagerFromStanza(stanza string) (*QueueManager, error) {
scanner := bufio.NewScanner(strings.NewReader(stanza))
qm := QueueManager{}
for scanner.Scan() {
l := scanner.Text()
l = strings.TrimSpace(l)
t := strings.Split(l, "=")
switch t[0] {
case "Name":
qm.Name = t[1]
case "Prefix":
qm.Prefix = t[1]
case "Directory":
qm.Directory = t[1]
case "DataPath":
qm.DataPath = t[1]
case "InstallationName":
qm.InstallationName = t[1]
}
}
return &qm, scanner.Err()
}
// GetQueueManager returns queue manager configuration information
func GetQueueManager(name string) (*QueueManager, error) {
_, err := os.Stat("/var/mqm/mqs.ini")
if err != nil {
// Don't run dspmqinf, which will generate an FDC if mqs.ini isn't there yet
return nil, errors.New("dspmqinf should not be run before crtmqdir")
}
// dspmqinf essentially returns a subset of mqs.ini, but it's simpler to parse
out, _, err := command.Run("dspmqinf", "-o", "stanza", name)
if err != nil {
return nil, err
}
return getQueueManagerFromStanza(out)
}
// GetErrorLogDirectory returns the directory holding the error logs for the
// specified queue manager
func GetErrorLogDirectory(qm *QueueManager) string {
return pathutils.CleanPath(GetDataDirectory(qm), "errors")
}
// GetDataDirectory returns the data directory for the specified queue manager
func GetDataDirectory(qm *QueueManager) string {
if qm.DataPath != "" {
// Data path has been set explicitly (e.g. for multi-instance queue manager)
return qm.DataPath
} else {
return pathutils.CleanPath(qm.Prefix, "qmgrs", qm.Directory)
}
}

64
pkg/mqini/mqini_test.go Normal file
View File

@@ -0,0 +1,64 @@
/*
© Copyright IBM Corporation 2018, 2023
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 mqini
import (
"os"
"testing"
)
var getQueueManagerTests = []struct {
file string
name string
prefix string
directory string
errorLogDir string
}{
{"dspmqinf1.txt", "foo", "/var/mqm", "foo", "/var/mqm/qmgrs/foo/errors"},
{"dspmqinf2.txt", "a/b", "/var/mqm", "a&b", "/var/mqm/qmgrs/a&b/errors"},
{"dspmqinf3.txt", "..", "/var/mqm", "!!", "/var/mqm/qmgrs/!!/errors"},
}
func TestGetQueueManager(t *testing.T) {
for _, table := range getQueueManagerTests {
t.Run(table.file, func(t *testing.T) {
b, err := os.ReadFile(table.file)
if err != nil {
t.Fatal(err)
}
qm, err := getQueueManagerFromStanza(string(b))
if err != nil {
t.Fatal(err)
}
t.Logf("%#v", qm)
if qm.Name != table.name {
t.Errorf("Expected name=%v; got %v", table.name, qm.Name)
}
if qm.Prefix != table.prefix {
t.Errorf("Expected prefix=%v; got %v", table.prefix, qm.Prefix)
}
if qm.Directory != table.directory {
t.Errorf("Expected directory=%v; got %v", table.directory, qm.Directory)
}
// Test
d := GetErrorLogDirectory(qm)
if d != table.errorLogDir {
t.Errorf("Expected error log directory=%v; got %v", table.errorLogDir, d)
}
})
}
}

46
pkg/name/name.go Normal file
View File

@@ -0,0 +1,46 @@
/*
© Copyright IBM Corporation 2017, 2019
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"
)
// sanitizeQueueManagerName removes any invalid characters from a queue manager name
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
}

56
pkg/name/name_test.go Normal file
View File

@@ -0,0 +1,56 @@
/*
© Copyright IBM Corporation 2017, 2019
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)
}
}