diff --git a/.travis.yml b/.travis.yml index 6da8df6..0defd61 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -dist: xenial +dist: bionic +group: beta sudo: required language: go -group: xeniallegacy go: - "1.13.15" diff --git a/Dockerfile-server b/Dockerfile-server index 892f539..6e3d72e 100644 --- a/Dockerfile-server +++ b/Dockerfile-server @@ -1,4 +1,4 @@ -# © Copyright IBM Corporation 2015, 2020 +# © Copyright IBM Corporation 2015, 2021 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -47,7 +47,6 @@ RUN go build ./cmd/chkmqready/ RUN go build ./cmd/chkmqhealthy/ RUN go build ./cmd/chkmqstarted/ RUN go build ./cmd/runmqdevserver/ -RUN go build -buildmode=c-shared -o amqpasdev.so ./internal/qmgrauth/pas.go RUN go test -v ./cmd/runmqdevserver/... RUN go test -v ./cmd/runmqserver/ RUN go test -v ./cmd/chkmqready/ @@ -118,6 +117,27 @@ USER 1001 ENV MQ_CONNAUTH_USE_HTP=false ENTRYPOINT ["runmqserver"] +############################################################################### +# Build stage to build C code for custom authorization service (developer-only) +############################################################################### +FROM registry.redhat.io/rhel8/gcc-toolset-9-toolchain as cbuilder +# The URL to download the MQ installer from in tar.gz format +# This assumes an archive containing the MQ Non-Install packages +ARG MQ_URL +USER 0 +# Install the Apache Portable Runtime code (used for htpasswd hash checking) +RUN yum -y install apr-devel apr-util-openssl apr-util-devel +# Install MQ client +COPY install-mq.sh /usr/local/bin/ +RUN mkdir /opt/mqm \ + && chmod a+x /usr/local/bin/install-mq.sh \ + && sleep 1 \ + && INSTALL_SDK=1 install-mq.sh \ + && chown -R 1001:root /opt/mqm/* +COPY authservice/ /opt/app-root/src/authservice/ +WORKDIR /opt/app-root/src/authservice/mqhtpass +RUN make all + ############################################################################### # Add default developer config ############################################################################### @@ -139,7 +159,7 @@ LABEL io.k8s.description="Simplify, accelerate and facilitate the reliable excha LABEL base-image=$BASE_IMAGE LABEL base-image-release=$BASE_TAG USER 0 -COPY --from=builder $GO_WORKDIR/amqpas* /opt/mqm/lib64/ +COPY --from=cbuilder /opt/app-root/src/authservice/mqhtpass/build/mqhtpass.so /opt/mqm/lib64/ COPY etc/mqm/*.ini /etc/mqm/ COPY etc/mqm/mq.htpasswd /etc/mqm/ RUN chmod 0660 /etc/mqm/mq.htpasswd diff --git a/Makefile b/Makefile index ca0b86e..aacd87b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# © Copyright IBM Corporation 2017, 2020 +# © Copyright IBM Corporation 2017, 2021 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -71,6 +71,8 @@ LTS ?= false ############################################################################### # Other variables ############################################################################### +# Build doesn't work if BuildKit is enabled +DOCKER_BUILDKIT=0 GO_PKG_DIRS = ./cmd ./internal ./test MQ_ARCHIVE_TYPE=LINUX MQ_ARCHIVE_DEV_TYPE=Linux diff --git a/authservice/mqhtpass/Makefile b/authservice/mqhtpass/Makefile new file mode 100644 index 0000000..01939f1 --- /dev/null +++ b/authservice/mqhtpass/Makefile @@ -0,0 +1,50 @@ +# © Copyright IBM Corporation 2017, 2020 +# +# 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. + +# This Makefile expects the following to be installed: +# - gcc +# - ldd +# - MQ SDK (mqm_r library, plus header files) +# - Apache Portable Runtime (apr-1 and aprutil-1 libraries, plus header files) + +SRC_DIR = src +BUILD_DIR = ./build + +# Flags passed to the C compiler. Need to use gnu11 to get POSIX functions needed for file locking. +CFLAGS += -std=gnu11 -fPIC -Wall -m64 + +LIB_APR = -L/usr/lib64 -lapr-1 -laprutil-1 +LIB_MQ = -L/opt/mqm/lib64 -lmqm_r + +all: $(BUILD_DIR)/mqhtpass.so $(BUILD_DIR)/htpass_test + +$(BUILD_DIR)/log.o : $(SRC_DIR)/log.c $(SRC_DIR)/log.h + mkdir -p ${dir $@} + gcc $(CFLAGS) -c $(SRC_DIR)/log.c -o $@ + +$(BUILD_DIR)/htpass.o : $(SRC_DIR)/htpass.c $(SRC_DIR)/htpass.h + mkdir -p ${dir $@} + gcc $(CFLAGS) -c $(SRC_DIR)/htpass.c -I /usr/include/apr-1 -o $@ + +$(BUILD_DIR)/htpass_test : $(BUILD_DIR)/htpass.o $(BUILD_DIR)/log.o + mkdir -p ${dir $@} + gcc $(CFLAGS) $(LIB_APR) -lpthread $(SRC_DIR)/htpass_test.c $^ -o $@ +# Run HTPasswd tests, and print log if they fail + $@ || (cat htpass_test*.log && exit 1) + +$(BUILD_DIR)/mqhtpass.so : $(BUILD_DIR)/log.o $(BUILD_DIR)/htpass.o + mkdir -p ${dir $@} + # NOTE: rpath for libapr will be different on Ubuntu + gcc $(CFLAGS) -I/opt/mqm/inc -D_REENTRANT $(LIB_APR) $(LIB_MQ) -Wl,-rpath,/opt/mqm/lib64 -Wl,-rpath,/usr/lib64 -shared $(SRC_DIR)/mqhtpass.c $^ -o $@ + ldd $@ diff --git a/authservice/mqhtpass/src/htpass.c b/authservice/mqhtpass/src/htpass.c new file mode 100644 index 0000000..a4df731 --- /dev/null +++ b/authservice/mqhtpass/src/htpass.c @@ -0,0 +1,145 @@ +/* +© Copyright IBM Corporation 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. +*/ + +#include +#include +#include +#include +#include +#include "log.h" +#include "htpass.h" +#include +#include +#include +#include + +bool htpass_valid_file(char *filename) +{ + bool valid = true; + FILE *fp; + char *huser; + + fp = fopen(filename, "r"); + if (fp == NULL) + { + log_errorf("Error %d opening htpasswd file '%s'", errno, filename); + } + if (fp) + { + const size_t line_size = 1024; + char *line = malloc(line_size); + while (fgets(line, line_size, fp) != NULL) + { + char *saveptr; + // Need to use strtok_r to be safe for multiple threads + huser = strtok_r(line, ":", &saveptr); + if (strlen(huser) >= 12) + { + log_errorf("Invalid htpasswd file for use with IBM MQ. User '%s' is longer than twelve characters", huser); + valid = false; + break; + } + } + fclose(fp); + if (line) + { + free(line); + } + } + return valid; +} + +char *find_hash(char *filename, char *user) +{ + bool found = false; + FILE *fp; + char *huser; + char *hash; + + fp = fopen(filename, "r"); + if (fp == NULL) + { + log_errorf("Error %d opening htpasswd file '%s'", errno, filename); + } + if (fp) + { + const size_t line_size = 1024; + char *line = malloc(line_size); + while (fgets(line, line_size, fp) != NULL) + { + char *saveptr; + // Need to use strtok_r to be safe for multiple threads + huser = strtok_r(line, ":", &saveptr); + if (huser && (strcmp(user, huser) == 0)) + { + // Make a duplicate of the string, because we'll be keeping it + hash = strdup(strtok_r(NULL, " \r\n\t", &saveptr)); + found = true; + break; + } + } + fclose(fp); + if (line) + { + free(line); + } + } + if (!found) + { + hash = NULL; + } + return hash; +} + +int htpass_authenticate_user(char *filename, char *user, char *password) +{ + char *hash = find_hash(filename, user); + int result = -1; + if (hash == NULL) + { + result = HTPASS_INVALID_USER; + log_debugf("User does not exist. user=%s", user); + } + else + { + // Use the Apache Portable Runtime utilities to validate the password against the hash. + // Supports multiple hashing algorithms, but we should only be using bcrypt + apr_status_t status = apr_password_validate(password, hash); + // status is usually either APR_SUCCESS or APR_EMISMATCH + if (status == APR_SUCCESS) + { + result = HTPASS_VALID; + log_debugf("Correct password supplied. user=%s", user); + } + else + { + result = HTPASS_INVALID_PASSWORD; + log_debugf("Incorrect password supplied. user=%s", user); + } + } + return result; +} + +bool htpass_valid_user(char *filename, char *user) +{ + char *hash = find_hash(filename, user); + bool valid = false; + if (hash != NULL) + { + valid = true; + } + return valid; +} diff --git a/authservice/mqhtpass/src/htpass.h b/authservice/mqhtpass/src/htpass.h new file mode 100644 index 0000000..305cd47 --- /dev/null +++ b/authservice/mqhtpass/src/htpass.h @@ -0,0 +1,49 @@ +/* +© Copyright IBM Corporation 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. +*/ + +#ifndef _HTPASS_H +#define _HTPASS_H + +#define HTPASS_VALID 0 +#define HTPASS_INVALID_USER 1 +#define HTPASS_INVALID_PASSWORD 2 + +/** + * Validate an HTPasswd file for use with IBM MQ. + * + * @param filename the HTPasswd file + */ +_Bool htpass_valid_file(char *filename); + +/** + * Authenticate a user, based on the supplied file name. + * + * @param filename the HTPasswd file + * @param user the user name to authenticate + * @param password the password of the user + * @return HTPASS_VALID, HTPASS_INVALID_USER or HTPASS_INVALID_PASSWORD + */ +int htpass_authenticate_user(char *filename, char *user, char *password); + +/** + * Validate that a user exists in the password file. + * + * @param filename the HTPasswd file + * @param user the user name to validate + */ +_Bool htpass_valid_user(char *filename, char *user); + +#endif \ No newline at end of file diff --git a/authservice/mqhtpass/src/htpass_test.c b/authservice/mqhtpass/src/htpass_test.c new file mode 100644 index 0000000..30b611b --- /dev/null +++ b/authservice/mqhtpass/src/htpass_test.c @@ -0,0 +1,223 @@ +/* +© Copyright IBM Corporation 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. +*/ + +#include +#include +#include +#include "log.h" +#include "htpass.h" + +// Headers for multi-threaded tests +#include + +// Start a test and log the function name +#define test_start() printf("=== RUN: %s\n", __func__) + +// Indicate test has passed +#define test_pass() printf("--- PASS: %s\n", __func__) + +// Indicate test has failed +void test_fail(const char *test_name) +{ + printf("--- FAIL: %s\n", test_name); + exit(1); +} + +// ---------------------------------------------------------------------------- +// Simple tests for file validation +// ---------------------------------------------------------------------------- + +void test_htpass_valid_file_ok() +{ + test_start(); + int ok = htpass_valid_file("./src/htpass_test.htpasswd"); + if (!ok) + test_fail(__func__); + test_pass(); +} + +void test_htpass_valid_file_too_long() +{ + test_start(); + int ok = htpass_valid_file("./src/htpass_test_invalid.htpasswd"); + if (ok) + test_fail(__func__); + test_pass(); +} + +// ---------------------------------------------------------------------------- +// Simple tests for authentication +// ---------------------------------------------------------------------------- + +void test_htpass_authenticate_user_fred_valid() +{ + test_start(); + int rc = htpass_authenticate_user("./src/htpass_test.htpasswd", "fred", "passw0rd"); + printf("%s: fred - %d\n", __func__, rc); + if (rc != HTPASS_VALID) + test_fail(__func__); + test_pass(); +} + +void test_htpass_authenticate_user_fred_invalid1() +{ + test_start(); + int rc = htpass_authenticate_user("./src/htpass_test.htpasswd", "fred", "passw0rd "); + printf("%s: fred - %d\n", __func__, rc); + if (rc != HTPASS_INVALID_PASSWORD) + test_fail(__func__); + test_pass(); +} + +void test_htpass_authenticate_user_fred_invalid2() +{ + test_start(); + int rc = htpass_authenticate_user("./src/htpass_test.htpasswd", "fred", ""); + printf("%s: fred - %d\n", __func__, rc); + if (rc != HTPASS_INVALID_PASSWORD) + test_fail(__func__); + test_pass(); +} + +void test_htpass_authenticate_user_fred_invalid3() +{ + test_start(); + int rc = htpass_authenticate_user("./src/htpass_test.htpasswd", "fred", "clearlywrong"); + printf("%s: fred - %d\n", __func__, rc); + if (rc != HTPASS_INVALID_PASSWORD) + test_fail(__func__); + test_pass(); +} + +void test_htpass_authenticate_user_barney_valid() +{ + test_start(); + int rc = htpass_authenticate_user("./src/htpass_test.htpasswd", "barney", "s3cret"); + printf("%s: barney - %d\n", __func__, rc); + if (rc != HTPASS_VALID) + test_fail(__func__); + test_pass(); +} + +void test_htpass_authenticate_user_unknown() +{ + test_start(); + int rc = htpass_authenticate_user("./src/htpass_test.htpasswd", "george", "s3cret"); + printf("%s: barney - %d\n", __func__, rc); + if (rc != HTPASS_INVALID_USER) + test_fail(__func__); + test_pass(); +} + +// ---------------------------------------------------------------------------- +// Multi-threaded test +// ---------------------------------------------------------------------------- + +#define NUM_THREADS 5 +// Number of tests to perform per thread. Higher numbers are more likely to trigger timing issue. +#define NUM_TESTS_PER_THREAD 1000 +// Maximum number of JSON errors to report (log can get flooded) +#define MAX_JSON_ERRORS 10 + +// Authenticate multiple users, multiple times +void *authenticate_many_times(void *p) +{ + for (int i = 0; i < NUM_TESTS_PER_THREAD; i++) + { + int rc = htpass_authenticate_user("./src/htpass_test.htpasswd", "barney", "s3cret"); + if (rc != HTPASS_VALID) + test_fail(__func__); + rc = htpass_authenticate_user("./src/htpass_test.htpasswd", "fred", "passw0rd"); + if (rc != HTPASS_VALID) + test_fail(__func__); + } + pthread_exit(NULL); +} + +void check_log_file_valid(char *filename) +{ + int errors = 0; + printf("--- Checking log file is valid\n"); + // Check that the JSON log file isn't corrupted + FILE *log = fopen(filename, "r"); + if (log == NULL) + { + test_fail(__func__); + } + const size_t line_size = 1024; + char *line = malloc(line_size); + while (fgets(line, line_size, log) != NULL) + { + if ((line[0] != '{') && (errors < MAX_JSON_ERRORS)) + { + printf("*** Invalid JSON detected: %s\n", line); + errors++; + } + } + if (line) + { + free(line); + } + fclose(log); +} + +// Test authenticate_user with multiple threads, each doing many authentications +void test_htpass_authenticate_user_multithreaded(char *logfile) +{ + pthread_t threads[NUM_THREADS]; + int rc; + test_start(); + // Re-initialize the log to use a file for the multi-threaded test + log_init(logfile); + for (int i = 0; i < NUM_THREADS; i++) + { + printf("Creating thread %d\n", i); + rc = pthread_create(&threads[i], NULL, authenticate_many_times, NULL); + if (rc) + { + printf("Error: Unable to create thread, %d\n", rc); + test_fail(__func__); + } + } + // Wait for all the threads to complete + for (int i = 0; i < NUM_THREADS; i++) + { + pthread_join(threads[i], NULL); + } + check_log_file_valid(logfile); + test_pass(); +} + +// ---------------------------------------------------------------------------- + +int main() +{ + // Turn on debugging for the tests + setenv("DEBUG", "true", true); + log_init("htpass_test.log"); + test_htpass_valid_file_ok(); + test_htpass_valid_file_too_long(); + test_htpass_authenticate_user_fred_valid(); + test_htpass_authenticate_user_fred_invalid1(); + test_htpass_authenticate_user_fred_invalid2(); + test_htpass_authenticate_user_fred_invalid3(); + test_htpass_authenticate_user_barney_valid(); + test_htpass_authenticate_user_unknown(); + log_close(); + + // Call multi-threaded test last, because it re-initializes the log to use a file + test_htpass_authenticate_user_multithreaded("htpass_test_multithreaded.log"); +} \ No newline at end of file diff --git a/authservice/mqhtpass/src/htpass_test.htpasswd b/authservice/mqhtpass/src/htpass_test.htpasswd new file mode 100644 index 0000000..5c1399b --- /dev/null +++ b/authservice/mqhtpass/src/htpass_test.htpasswd @@ -0,0 +1,2 @@ +fred:$2y$05$3Fp9epsqEwWOHdyj9Ngf9.qfX34kzc9zNrdQ7kac0GmcCvQjIkAwy +barney:$2y$05$l8EoyCQ9y2PyfUzIDDfTyu7SSaJEYB1TuHy07xZvN7xt/pR3SIw0a diff --git a/authservice/mqhtpass/src/htpass_test_invalid.htpasswd b/authservice/mqhtpass/src/htpass_test_invalid.htpasswd new file mode 100644 index 0000000..4a01c74 --- /dev/null +++ b/authservice/mqhtpass/src/htpass_test_invalid.htpasswd @@ -0,0 +1,3 @@ +fred:$2y$05$3Fp9epsqEwWOHdyj9Ngf9.qfX34kzc9zNrdQ7kac0GmcCvQjIkAwy +barney:$2y$05$l8EoyCQ9y2PyfUzIDDfTyu7SSaJEYB1TuHy07xZvN7xt/pR3SIw0a +namewhichisfartoolongformq:$2y$05$l8EoyCQ9y2PyfUzIDDfTyu7SSaJEYB1TuHy07xZvN7xt/pR3SIw0a \ No newline at end of file diff --git a/authservice/mqhtpass/src/log.c b/authservice/mqhtpass/src/log.c new file mode 100644 index 0000000..70675c0 --- /dev/null +++ b/authservice/mqhtpass/src/log.c @@ -0,0 +1,152 @@ +/* +© Copyright IBM Corporation 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. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +FILE *fp = NULL; +int pid; +char hostname[255]; +bool debug = false; + +/** + * Determine whether debugging is enabled or not, using an environment variable. + */ +void init_debug(){ + char *debug_env = getenv("DEBUG"); + if (debug_env != NULL) + { + // Enable debug logging if the DEBUG environment variable is set + if (strncmp(debug_env, "true", 4) || strncmp(debug_env, "1", 1)) + { + debug = true; + } + } +} + +/** + * Internal function to initialize the log with the given file mode. + */ +int log_init_internal(char *filename, const char *mode) +{ + int result = 0; + pid = getpid(); + hostname[254] = '\0'; + gethostname(hostname, 254); + if (!fp) + { + fp = fopen(filename, "a"); + if (fp) + { + // Disable buffering for this file + setbuf(fp, NULL); + } + else + { + result = 1; + } + } + init_debug(); + return result; +} + +int log_init_reset(char *filename) +{ + // Open the log file for writing (overwrite if it already exists) + return log_init_internal(filename, "w"); +} + +int log_init(char *filename) +{ + // Open the log file file for appending + return log_init_internal(filename, "a"); +} + +void log_init_file(FILE *f) +{ + fp = f; + init_debug(); +} + +void log_close() +{ + if (fp) + { + fclose(fp); + fp = NULL; + } +} + +void log_printf(const char *source_file, int source_line, const char *level, const char *format, ...) +{ + if (fp) + { + // If this is a DEBUG message, and debugging is off + if ((strncmp(level, "DEBUG", 5) == 0) && !debug) + { + return; + } + char buf[1024] = ""; + char *cur = buf; + char* const end = buf + sizeof buf; + char date_buf[70]; + struct tm *utc; + time_t t; + struct timeval now; + + gettimeofday(&now, NULL); + t = now.tv_sec; + t = time(NULL); + utc = gmtime(&t); + + cur += snprintf(cur, end-cur, "{"); + cur += snprintf(cur, end-cur, "\"loglevel\":\"%s\"", level); + // Print ISO-8601 time and date + if (strftime(date_buf, sizeof date_buf, "%FT%T", utc)) + { + // Round microseconds down to milliseconds, for consistency + cur += snprintf(cur, end-cur, ", \"ibm_datetime\":\"%s.%03ldZ\"", date_buf, now.tv_usec / 1000); + } + cur += snprintf(cur, end-cur, ", \"ibm_processId\":\"%d\"", pid); + cur += snprintf(cur, end-cur, ", \"host\":\"%s\"", hostname); + cur += snprintf(cur, end-cur, ", \"module\":\"%s:%d\"", source_file, source_line); + cur += snprintf(cur, end-cur, ", \"message\":\""); + + if (strncmp(level, "DEBUG", 5) == 0) + { + // Add a prefix on any debug messages + cur += snprintf(cur, end-cur, "mqhtpass: "); + } + + // Print log message, using varargs + va_list args; + va_start(args, format); + cur += vsnprintf(cur, end-cur, format, args); + va_end(args); + cur += snprintf(cur, end-cur, "\"}\n"); + + // Important: Just do one file write, to prevent problems with multi-threading. + // This only works if the log message is not too long for the buffer. + fprintf(fp, buf); + } +} + diff --git a/authservice/mqhtpass/src/log.h b/authservice/mqhtpass/src/log.h new file mode 100644 index 0000000..fa92135 --- /dev/null +++ b/authservice/mqhtpass/src/log.h @@ -0,0 +1,63 @@ +/* +© Copyright IBM Corporation 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. +*/ + +#ifndef _LOG_H +#define _LOG_H + +/** + * Initialize the log to use the given file name, wiping any existing contents. + */ +int log_init_reset(char *filename); + +/** + * Initialize the log to use the given file name. + */ +int log_init(char *filename); + +/** + * Initialize the log with an existing file handle. + */ +void log_init_file(FILE *f); + +/** + * Write a message to the log file, based on a printf format string. + * + * @param source_file the name of the source code file submitting this log message + * @param source_line the line of code in the source file + * @param level the log level, one of "DEBUG", "INFO" or "ERROR" + * @param format the printf format string for the message + */ +void log_printf(const char *source_file, int source_line, const char *level, const char *format, ...); + +void log_close(); + +/** + * Variadic macro to write an informational message to the log file, based on a printf format string. + */ +#define log_infof(format,...) log_printf(__FILE__, __LINE__, "INFO", format, ##__VA_ARGS__) + +/** + * Variadic macro to write an error message to the log file, based on a printf format string. + */ +#define log_errorf(format,...) log_printf(__FILE__, __LINE__, "ERROR", format, ##__VA_ARGS__) + +/** + * Variadic macro to write a debug message to the log file, based on a printf format string. + */ +#define log_debugf(format,...) log_printf(__FILE__, __LINE__, "DEBUG", format, ##__VA_ARGS__) + + +#endif \ No newline at end of file diff --git a/authservice/mqhtpass/src/mqhtpass.c b/authservice/mqhtpass/src/mqhtpass.c new file mode 100644 index 0000000..516e6ce --- /dev/null +++ b/authservice/mqhtpass/src/mqhtpass.c @@ -0,0 +1,351 @@ +/* +© Copyright IBM Corporation 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. +*/ + +// This is a developer only configuration and not recommended for production usage. + +#include +#include +#include +#include +#include +#include "log.h" +#include "htpass.h" + +// Declare the internal functions that implement the interface +MQZ_INIT_AUTHORITY MQStart; +static MQZ_AUTHENTICATE_USER mqhtpass_authenticate_user; +static MQZ_FREE_USER mqhtpass_free_user; +static MQZ_TERM_AUTHORITY mqhtpass_terminate; + +#define LOG_FILE "/var/mqm/errors/mqhtpass.json" +#define HTPASSWD_FILE "/etc/mqm/mq.htpasswd" +#define NAME "MQ Advanced for Developers custom authentication service" + +static char *trim(char *s); + +/** + * Initialization and entrypoint for the dynamically loaded + * authorization installable service. It registers the addresses of the + * other functions which are to be called by the queue manager. + * + * This function is called whenever the module is loaded. The Options + * field will show whether it's a PRIMARY (i.e. during qmgr startup) or + * SECONDARY. + */ +void MQENTRY MQStart( + MQHCONFIG hc, + MQLONG Options, + MQCHAR48 QMgrName, + MQLONG ComponentDataLength, + PMQBYTE ComponentData, + PMQLONG Version, + PMQLONG pCompCode, + PMQLONG pReason) +{ + MQLONG CC = MQCC_OK; + MQLONG Reason = MQRC_NONE; + int log_rc = 0; + + if (Options == MQZIO_PRIMARY) + { + // Reset the log file. The file could still get large if debug is turned on, + // but this is a simpler solution for now. + log_rc = log_init_reset(LOG_FILE); + } + else + { + log_rc = log_init(LOG_FILE); + } + + if (log_rc != 0) + { + CC = MQCC_FAILED; + Reason = MQRC_INITIALIZATION_FAILED; + } + + if (Options == MQZIO_PRIMARY) + { + log_infof("Initializing %s", NAME); + } + log_debugf("MQStart options=%s qmgr=%s", ((Options == MQZIO_SECONDARY) ? "Secondary" : "Primary"), trim(QMgrName)); + + if (!htpass_valid_file(HTPASSWD_FILE)) + { + CC = MQCC_FAILED; + Reason = MQRC_INITIALIZATION_FAILED; + } + + // Initialize the functions to use for each entry point + if (CC == MQCC_OK) + { + hc->MQZEP_Call(hc, MQZID_INIT_AUTHORITY, (PMQFUNC)MQStart, &CC, &Reason); + } + if (CC == MQCC_OK) + { + hc->MQZEP_Call(hc, MQZID_TERM_AUTHORITY, (PMQFUNC)mqhtpass_terminate, &CC, &Reason); + } + if (CC == MQCC_OK) + { + hc->MQZEP_Call(hc, MQZID_AUTHENTICATE_USER, (PMQFUNC)mqhtpass_authenticate_user, &CC, &Reason); + } + if (CC == MQCC_OK) + { + hc->MQZEP_Call(hc, MQZID_FREE_USER, (PMQFUNC)mqhtpass_free_user, &CC, &Reason); + } + *Version = MQZAS_VERSION_5; + *pCompCode = CC; + *pReason = Reason; + return; +} + +/** + * Called during the connection of any application which supplies an MQCSP (Connection Security Parameters). + * This is the usual case. + * See https://www.ibm.com/support/knowledgecenter/SSFKSJ_latest/com.ibm.mq.ref.dev.doc/q095610_.html + */ +static void MQENTRY mqhtpass_authenticate_user_csp( + PMQCHAR pQMgrName, + PMQCSP pSecurityParms, + PMQZAC pApplicationContext, + PMQZIC pIdentityContext, + PMQPTR pCorrelationPtr, + PMQBYTE pComponentData, + PMQLONG pContinuation, + PMQLONG pCompCode, + PMQLONG pReason) +{ + char *csp_user = NULL; + char *csp_pass = NULL; + + // Firstly, create null-terminated strings from the user credentials in the MQ CSP object + csp_user = malloc(pSecurityParms->CSPUserIdLength + 1); + if (!csp_user) + { + log_errorf("%s is unable to allocate memory for a user", NAME); + *pCompCode = MQCC_FAILED; + *pReason = MQRC_SERVICE_ERROR; + return; + } + strncpy(csp_user, pSecurityParms->CSPUserIdPtr, pSecurityParms->CSPUserIdLength); + csp_user[pSecurityParms->CSPUserIdLength] = 0; + csp_pass = malloc((pSecurityParms->CSPPasswordLength + 1)); + if (!csp_pass) + { + log_errorf("%s is unable to allocate memory for a password", NAME); + *pCompCode = MQCC_FAILED; + *pReason = MQRC_SERVICE_ERROR; + if (csp_user) + { + free(csp_user); + } + return; + } + strncpy(csp_pass, pSecurityParms->CSPPasswordPtr, pSecurityParms->CSPPasswordLength); + csp_pass[pSecurityParms->CSPPasswordLength] = 0; + log_debugf("%s with CSP user set. user=%s", __func__, csp_user); + int auth_result = htpass_authenticate_user(HTPASSWD_FILE, csp_user, csp_pass); + + if (auth_result == HTPASS_VALID) + { + // An OK completion code means MQ will accept this user is authenticated + *pCompCode = MQCC_OK; + *pReason = MQRC_NONE; + // Tell the queue manager to stop trying other authorization services. + *pContinuation = MQZCI_STOP; + memcpy(pIdentityContext->UserIdentifier, csp_user, sizeof(pIdentityContext->UserIdentifier)); + log_debugf("Authenticated user=%s", pIdentityContext->UserIdentifier); + } + // If the htpasswd file does not have an entry for this user + else if (auth_result == HTPASS_INVALID_USER) + { + *pCompCode = MQCC_WARNING; + *pReason = MQRC_NONE; + // Tell the queue manager to continue trying other authorization services, as they might have the user. + *pContinuation = MQZCI_CONTINUE; + log_debugf( + "User authentication failed due to invalid user. user=%s effuser=%s applname=%s csp_user=%s cc=%d reason=%d", + trim(pIdentityContext->UserIdentifier), + trim(pApplicationContext->EffectiveUserID), + trim(pApplicationContext->ApplName), + trim(csp_user), + *pCompCode, + *pReason); + } + // If the htpasswd file has an entry for this user, but the password supplied is incorrect + else if (auth_result == HTPASS_INVALID_PASSWORD) + { + *pCompCode = MQCC_WARNING; + *pReason = MQRC_NOT_AUTHORIZED; + // Tell the queue manager to stop trying other authorization services. + *pContinuation = MQZCI_STOP; + log_debugf( + "User authentication failed due to invalid password. user=%s effuser=%s applname=%s csp_user=%s cc=%d reason=%d", + trim(pIdentityContext->UserIdentifier), + trim(pApplicationContext->EffectiveUserID), + trim(pApplicationContext->ApplName), + trim(csp_user), + *pCompCode, + *pReason); + } + if (csp_user) + { + free(csp_user); + } + if (csp_pass) + { + free(csp_pass); + } + return; +} + +/** + * Called during the connection of any application. + * For more information on the parameters, see https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_latest/com.ibm.mq.ref.dev.doc/q110090_.html + */ +static void MQENTRY mqhtpass_authenticate_user( + PMQCHAR pQMgrName, + PMQCSP pSecurityParms, + PMQZAC pApplicationContext, + PMQZIC pIdentityContext, + PMQPTR pCorrelationPtr, + PMQBYTE pComponentData, + PMQLONG pContinuation, + PMQLONG pCompCode, + PMQLONG pReason) +{ + char *spuser = NULL; + char *sppass = NULL; + // By default, return a warning, which indicates to MQ that this + // authorization service hasn't authenticated the user. + *pCompCode = MQCC_WARNING; + *pReason = MQRC_NONE; + // By default, tell the queue manager to continue trying other + // authorization services. + *pContinuation = MQZCI_CONTINUE; + + if ((pSecurityParms->AuthenticationType) == MQCSP_AUTH_USER_ID_AND_PWD) + { + mqhtpass_authenticate_user_csp(pQMgrName, pSecurityParms, pApplicationContext, pIdentityContext, pCorrelationPtr, pComponentData, pContinuation, pCompCode, pReason); + } + else + { + // Password not supplied, so just check that the user ID is valid + spuser = malloc(sizeof(PMQCHAR12) + 1); + if (!spuser) + { + log_errorf("%s is unable to allocate memory to check a user", NAME); + *pCompCode = MQCC_FAILED; + *pReason = MQRC_SERVICE_ERROR; + return; + } + strncpy(spuser, pApplicationContext->EffectiveUserID, strlen(pApplicationContext->EffectiveUserID)); + spuser[sizeof(PMQCHAR12)] = 0; + log_debugf("%s without CSP user set. effectiveuid=%s env=%d, callertype=%d, type=%d, accttoken=%d applidentitydata=%d", __func__, spuser, pApplicationContext->Environment, pApplicationContext->CallerType, pApplicationContext->AuthenticationType, pIdentityContext->AccountingToken, pIdentityContext->ApplIdentityData); + if (strncmp(spuser, "mqm", 3) == 0) + { + // Special case: pass the "mqm" user on for validation up the chain + // A warning in the completion code means MQ will pass this to other authorization services + *pCompCode = MQCC_WARNING; + *pReason = MQRC_NONE; + *pContinuation = MQZCI_CONTINUE; + } + else + { + bool valid_user = htpass_valid_user(HTPASSWD_FILE, spuser); + if (valid_user) + { + // An OK completion code means MQ will accept this user is authenticated + *pCompCode = MQCC_OK; + *pReason = MQRC_NONE; + *pContinuation = MQZCI_STOP; + memcpy(pIdentityContext->UserIdentifier, spuser, sizeof(pIdentityContext->UserIdentifier)); + } + else + { + log_debugf( + "User authentication failed user=%s effuser=%s applname=%s cspuser=%s cc=%d reason=%d", + trim(pIdentityContext->UserIdentifier), + trim(pApplicationContext->EffectiveUserID), + trim(pApplicationContext->ApplName), + trim(spuser), + *pCompCode, + *pReason); + } + if (spuser) + { + free(spuser); + } + } + } + return; +} + +/** + * Called during MQDISC, as the inverse of the call to authenticate. + */ +static void MQENTRY mqhtpass_free_user( + PMQCHAR pQMgrName, + PMQZFP pFreeParms, + PMQBYTE pComponentData, + PMQLONG pContinuation, + + PMQLONG pCompCode, + PMQLONG pReason) +{ + log_debugf("mqhtpass_freeuser()"); + *pCompCode = MQCC_WARNING; + *pReason = MQRC_NONE; + *pContinuation = MQZCI_CONTINUE; +} + +/** + * Called when the authorization service is terminated. + */ +static void MQENTRY mqhtpass_terminate( + MQHCONFIG hc, + MQLONG Options, + PMQCHAR pQMgrName, + PMQBYTE pComponentData, + PMQLONG pCompCode, + PMQLONG pReason) +{ + if (Options == MQZTO_PRIMARY) + { + log_infof("Terminating %s", NAME); + log_close(); + } + else { + log_debugf("Terminating secondary"); + } + *pCompCode = MQCC_OK; + *pReason = MQRC_NONE; +} + +/** + * Remove trailing spaces from a string. + */ +static char *trim(char *s) +{ + int i; + for (i = strlen(s) - 1; i >= 0; i--) + { + if (s[i] == ' ') + s[i] = 0; + else + break; + } + return s; +} diff --git a/cmd/runmqdevserver/main.go b/cmd/runmqdevserver/main.go index 51e7163..ff5a1a6 100644 --- a/cmd/runmqdevserver/main.go +++ b/cmd/runmqdevserver/main.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2018, 2020 +© 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. @@ -19,7 +19,6 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "syscall" "github.com/ibm-messaging/mq-container/internal/htpasswd" @@ -30,27 +29,6 @@ import ( var log *logger.Logger -func setPassword(user string, password string) error { - // #nosec G204 - cmd := exec.Command("sudo", "chpasswd") - stdin, err := cmd.StdinPipe() - if err != nil { - return err - } - fmt.Fprintf(stdin, "%s:%s", user, password) - err = stdin.Close() - if err != nil { - log.Errorf("Error closing password stdin: %v", err) - } - out, err := cmd.CombinedOutput() - if err != nil { - // Include the command output in the error - return fmt.Errorf("%v: %v", err.Error(), out) - } - log.Printf("Set password for \"%v\" user", user) - return nil -} - func getLogFormat() string { return os.Getenv("LOG_FORMAT") } diff --git a/cmd/runmqserver/logging.go b/cmd/runmqserver/logging.go index 776f4d4..3e196c9 100644 --- a/cmd/runmqserver/logging.go +++ b/cmd/runmqserver/logging.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2017, 2020 +© Copyright IBM Corporation 2017, 2021 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -79,6 +79,8 @@ func formatBasic(obj map[string]interface{}) string { if len(inserts) > 0 { return fmt.Sprintf("%s %s [%v]\n", obj["ibm_datetime"], obj["message"], strings.Join(inserts, ", ")) } + // Convert time zone information from some logs (e.g. Liberty) for consistency + obj["ibm_datetime"] = strings.Replace(obj["ibm_datetime"].(string), "+0000", "Z", 1) return fmt.Sprintf("%s %s\n", obj["ibm_datetime"], obj["message"]) } @@ -100,6 +102,16 @@ func mirrorQueueManagerErrorLogs(ctx context.Context, wg *sync.WaitGroup, name s return mirrorLog(ctx, wg, f, fromStart, mf, true) } +// mirrorHTPasswdLogs starts a goroutine to mirror the contents of the MQ HTPasswd authorization service's log +func mirrorHTPasswdLogs(ctx context.Context, wg *sync.WaitGroup, name string, fromStart bool, mf mirrorFunc) (chan error, error) { + return mirrorLog(ctx, wg, "/var/mqm/errors/mqhtpass.json", false, mf, true) +} + +// mirrorWebServerLogs starts a goroutine to mirror the contents of the Liberty web server messages.log +func mirrorWebServerLogs(ctx context.Context, wg *sync.WaitGroup, name string, fromStart bool, mf mirrorFunc) (chan error, error) { + return mirrorLog(ctx, wg, "/var/mqm/web/installations/Installation1/servers/mqweb/logs/messages.log", false, mf, true) +} + func getDebug() bool { debug := os.Getenv("DEBUG") if debug == "true" || debug == "1" { @@ -119,14 +131,21 @@ func configureLogger(name string) (mirrorFunc, error) { return nil, err } return func(msg string, isQMLog bool) bool { - obj, err := processLogMessage(msg) - if err == nil && isQMLog && filterQMLogMessage(obj) { - return false - } - if err != nil { - log.Printf("Failed to unmarshall JSON - %v", msg) + // Check if the message is JSON + if len(msg) > 0 && msg[0] == '{' { + obj, err := processLogMessage(msg) + if err == nil && isQMLog && filterQMLogMessage(obj) { + return false + } + if err != nil { + log.Printf("Failed to unmarshall JSON in log message - %v", msg) + } else { + fmt.Println(msg) + } } else { - fmt.Println(msg) + // The log being mirrored isn't JSON, so wrap it in a simple JSON message + // MQ error logs are usually JSON, but this is useful for Liberty logs - usually expect WLP_LOGGING_MESSAGE_FORMAT=JSON to be set when mirroring Liberty logs. + fmt.Printf("{\"message\":\"%s\"}\n", msg) } return true }, nil @@ -136,16 +155,22 @@ func configureLogger(name string) (mirrorFunc, error) { return nil, err } return func(msg string, isQMLog bool) bool { - // Parse the JSON message, and print a simplified version - obj, err := processLogMessage(msg) - if err == nil && isQMLog && filterQMLogMessage(obj) { - return false - } - if err != nil { - log.Printf("Failed to unmarshall JSON - %v", err) + // Check if the message is JSON + if len(msg) > 0 && msg[0] == '{' { + // Parse the JSON message, and print a simplified version + obj, err := processLogMessage(msg) + if err == nil && isQMLog && filterQMLogMessage(obj) { + return false + } + if err != nil { + log.Printf("Failed to unmarshall JSON in log message - %v", err) + } else { + fmt.Printf(formatBasic(obj)) + } } else { - fmt.Printf(formatBasic(obj)) - // fmt.Printf(formatSimple(obj["ibm_datetime"].(string), obj["message"].(string))) + // The log being mirrored isn't JSON, so just print it. + // MQ error logs are usually JSON, but this is useful for Liberty logs - usually expect WLP_LOGGING_MESSAGE_FORMAT=JSON to be set when mirroring Liberty logs. + fmt.Println(msg) } return true }, nil diff --git a/cmd/runmqserver/main.go b/cmd/runmqserver/main.go index 41137f5..703043d 100644 --- a/cmd/runmqserver/main.go +++ b/cmd/runmqserver/main.go @@ -197,6 +197,23 @@ func doMain() error { logTermination(err) return err } + if *devFlag { + _, err = mirrorHTPasswdLogs(ctx, &wg, name, newQM, mf) + if err != nil { + logTermination(err) + return err + } + } + // Recommended to use this option in conjunction with setting WLP_LOGGING_MESSAGE_FORMAT=JSON + mirrorWebLog := os.Getenv("MQ_ENABLE_EMBEDDED_WEB_SERVER_LOG") + if mirrorWebLog == "true" || mirrorWebLog == "1" { + _, err = mirrorWebServerLogs(ctx, &wg, name, newQM, mf) + if err != nil { + logTermination(err) + return err + } + } + err = updateCommandLevel() if err != nil { logTermination(err) diff --git a/docs/pluggable-connauth.md b/docs/pluggable-connauth.md index 6ea061b..e8a32d4 100644 --- a/docs/pluggable-connauth.md +++ b/docs/pluggable-connauth.md @@ -24,6 +24,6 @@ Use an administrative tool or your application to connect to queue manager using #### Troubleshooting -A log file named `amqpasdev.log` is generated under `/var/mqm/errors` directory path of the container. This file will contain all the failed connection authentication requests. +A log file named `mqhtpass.log` is generated under `/var/mqm/errors` directory path of the container. This file will contain all the failed connection authentication requests. Additional information is logged to this file if the environment variable `DEBUG` is set to `true`. -**Please note**: This log file is based on circular logging and the maximum size is restricted to 1MB. +**Please note**: This log file will be wiped when the queue manager is next started. diff --git a/docs/security.md b/docs/security.md index e3c961c..4d025ad 100644 --- a/docs/security.md +++ b/docs/security.md @@ -18,18 +18,3 @@ docker run \ --detach \ ibm-mqadvanced-server:9.2.2.0-amd64 ``` - -The MQ Advanced for Developers image does require the "chown", "setuid", "setgid" and "audit_write" capabilities (plus "dac_override" if you're using an image based on Red Hat Enterprise Linux). This is because it uses the "sudo" command to change passwords inside the container. For example, in Docker, you could do the following: - -```sh -docker run \ - --cap-drop=ALL \ - --cap-add=CHOWN \ - --cap-add=SETUID \ - --cap-add=SETGID \ - --cap-add=AUDIT_WRITE \ - --env LICENSE=accept \ - --env MQ_QMGR_NAME=QM1 \ - --detach \ - ibm-mqadvanced-server-dev:9.2.2.0-amd64 -``` diff --git a/etc/mqm/qm-service-component.ini b/etc/mqm/qm-service-component.ini index 7daf39c..a9bcd2b 100644 --- a/etc/mqm/qm-service-component.ini +++ b/etc/mqm/qm-service-component.ini @@ -1,7 +1,7 @@ ServiceComponent: Service=AuthorizationService Name=Dev.HtpAuth.Service - Module=/opt/mqm/lib64/amqpasdev.so + Module=/opt/mqm/lib64/mqhtpass.so ComponentDataSize=0 ServiceComponent: Service=AuthorizationService diff --git a/incubating/mqadvanced-server-dev/install-extra-packages.sh b/incubating/mqadvanced-server-dev/install-extra-packages.sh index 0535015..9fd3414 100644 --- a/incubating/mqadvanced-server-dev/install-extra-packages.sh +++ b/incubating/mqadvanced-server-dev/install-extra-packages.sh @@ -1,6 +1,6 @@ #!/bin/bash # -*- mode: sh -*- -# © Copyright IBM Corporation 2019 +# © Copyright IBM Corporation 2019, 2021 # # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,17 +25,17 @@ test -f /usr/bin/apt-get && UBUNTU=true || UBUNTU=false if ($UBUNTU); then export DEBIAN_FRONTEND=noninteractive apt-get update - apt-get install -y --no-install-recommends sudo + apt-get install -y --no-install-recommends libaprutil1 rm -rf /var/lib/apt/lists/* fi if ($YUM); then - yum -y install sudo + yum -y install apr-util-openssl yum -y clean all rm -rf /var/cache/yum/* fi if ($MICRODNF); then - microdnf install sudo - microdnf clean all + microdnf --disableplugin=subscription-manager install apr-util-openssl + microdnf --disableplugin=subscription-manager clean all fi \ No newline at end of file diff --git a/install-mq-server-prereqs.sh b/install-mq-server-prereqs.sh index b2385ae..bf96f7d 100644 --- a/install-mq-server-prereqs.sh +++ b/install-mq-server-prereqs.sh @@ -1,6 +1,6 @@ #!/bin/bash # -*- mode: sh -*- -# © Copyright IBM Corporation 2015, 2020 +# © Copyright IBM Corporation 2015, 2021 # # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -66,7 +66,7 @@ if ($RPM); then EXTRA_RPMS="bash bc ca-certificates file findutils gawk glibc-common grep ncurses-compat-libs passwd procps-ng sed shadow-utils tar util-linux which" # Install additional packages required by MQ, this install process and the runtime scripts $YUM && yum -y install --setopt install_weak_deps=false ${EXTRA_RPMS} - $MICRODNF && microdnf install ${EXTRA_RPMS} + $MICRODNF && microdnf --disableplugin=subscription-manager install ${EXTRA_RPMS} fi # Apply any bug fixes not included in base Ubuntu or MQ image. @@ -78,4 +78,4 @@ $UBUNTU && apt-get install -y libapparmor1 libsystemd0 systemd systemd-sysv libu $UBUNTU && rm -rf /var/lib/apt/lists/* $YUM && yum -y clean all $YUM && rm -rf /var/cache/yum/* -$MICRODNF && microdnf clean all +$MICRODNF && microdnf --disableplugin=subscription-manager clean all diff --git a/internal/htpasswd/htpasswd.go b/internal/htpasswd/htpasswd.go index 0ef5091..8d8a917 100644 --- a/internal/htpasswd/htpasswd.go +++ b/internal/htpasswd/htpasswd.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2020 +© Copyright IBM Corporation 2020, 2021 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -110,44 +110,3 @@ func (htpfile mapHtPasswd) updateHtPasswordFile(isTest bool) error { } return ioutil.WriteFile(file, htpfile.GetBytes(), 0660) } - -// AuthenticateUser verifies if the given user password match with htpasswrd -func AuthenticateUser(user string, password string, isTest bool) (bool, bool, error) { - passwords := mapHtPasswd(map[string]string{}) - - if len(strings.TrimSpace(user)) == 0 || len(strings.TrimSpace(password)) == 0 { - return false, false, fmt.Errorf("UserId or Password are empty") - } - - err := passwords.ReadHtPasswordFile(isTest) - if err != nil { - return false, false, err - } - - ok := false - value, found := passwords[user] - - if !found { - return found, ok, fmt.Errorf("User not found in the mq.htpasswd file") - } - - err = bcrypt.CompareHashAndPassword([]byte(value), []byte(password)) - return found, err == nil, err -} - -// ValidateUser validates the given user -func ValidateUser(user string, isTest bool) (bool, error) { - passwords := mapHtPasswd(map[string]string{}) - - if len(strings.TrimSpace(user)) == 0 { - return false, fmt.Errorf("Userid is empty for AuthenticateUser") - } - - err := passwords.ReadHtPasswordFile(isTest) - if err != nil { - return false, err - } - - _, found := passwords[strings.TrimSpace(user)] - return found, nil -} diff --git a/internal/htpasswd/htpasswd_test.go b/internal/htpasswd/htpasswd_test.go deleted file mode 100644 index 00027c6..0000000 --- a/internal/htpasswd/htpasswd_test.go +++ /dev/null @@ -1,62 +0,0 @@ -/* -© Copyright IBM Corporation 2020 - -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 htpasswd - -import ( - "testing" -) - -// TestCheckUser verifies Htpassword's use -func TestCheckUser(t *testing.T) { - err := SetPassword("guest", "guestpw", true) - if err != nil { - t.Fatalf("htpassword test failed due to error:%s\n", err.Error()) - } - found, ok, err := AuthenticateUser("guest", "guestpw", true) - if err != nil { - t.Fatalf("htpassword test1 failed as user could not be found:%s\n", err.Error()) - } - if found == false || ok == false { - t.Fatalf("htpassword test1 failed as user could not be found:%v, ok:%v\n", found, ok) - } - - found, ok, err = AuthenticateUser("myguest", "guestpw", true) - if err == nil { - t.Fatalf("htpassword test2 failed as no error received for non-existing user\n") - } - if found == true || ok == true { - t.Fatalf("htpassword test2 failed for non-existing user found :%v, ok:%v\n", found, ok) - } - - found, ok, err = AuthenticateUser("guest", "guest", true) - if err == nil { - t.Fatalf("htpassword test3 failed as incorrect password of user did not return error\n") - } - - if found == false || ok == true { - t.Fatalf("htpassword test3 failed for existing user with incorrect passwored found :%v, ok:%v\n", found, ok) - } - - found, err = ValidateUser("guest", true) - if err != nil || found == false { - t.Fatalf("htpassword test4 failed as user could not be found:%v, ok:%v\n", found, ok) - } - - found, err = ValidateUser("myguest", true) - if err != nil || found == true { - t.Fatalf("htpassword test5 failed as non-existing user returned to be found:%v, ok:%v\n", found, ok) - } -} diff --git a/internal/htpasswd/my.htpasswd b/internal/htpasswd/my.htpasswd deleted file mode 100644 index d9ce748..0000000 --- a/internal/htpasswd/my.htpasswd +++ /dev/null @@ -1 +0,0 @@ -guest:$2y$05$ifFP0nCmFed6.m4iB9CHRuHFps2YeeuwopmOvszWt0GRnN59p8qxW \ No newline at end of file diff --git a/internal/qmgrauth/pas.go b/internal/qmgrauth/pas.go deleted file mode 100644 index c6991cd..0000000 --- a/internal/qmgrauth/pas.go +++ /dev/null @@ -1,299 +0,0 @@ -/* -© Copyright IBM Corporation 2020 - -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. -*/ - -//This is a developer only configuration and not recommended for production usage. -package main - -/* -#cgo !windows CFLAGS: -I/opt/mqm/lib64 -D_REENTRANT -#cgo !windows,!darwin LDFLAGS: -L/opt/mqm/lib64 -lmqm_r -Wl,-rpath,/opt/mqm/lib64 -Wl,-rpath,/usr/lib64 -#cgo darwin LDFLAGS: -L/opt/mqm/lib64 -lmqm_r -Wl,-rpath,/opt/mqm/lib64 -Wl,-rpath,/usr/lib64 -#cgo windows CFLAGS: -I"C:/Program Files/IBM/MQ/Tools/c/include" -#cgo windows LDFLAGS: -L "C:/Program Files/IBM/MQ/bin64" -lmqm -#include -#include -#include -#include -#include -#include -#include -#include -static MQZ_INIT_AUTHORITY PASStart; -static MQZ_AUTHENTICATE_USER OAAuthUser; -static MQZ_FREE_USER OAFreeUser; -static MQZ_TERM_AUTHORITY OATermAuth; -extern int Authenticate(char *, char *); -extern int CheckAuthority(char *); -static char *OAEnvStr(MQLONG); -static void FindSize(); -static void PrintDateTime(); -static FILE *fp = NULL; -static int primary_process = 0; - -static void MQENTRY PASStart( - MQHCONFIG hc, - MQLONG Options, - MQCHAR48 QMgrName, - MQLONG ComponentDataLength, - PMQBYTE ComponentData, - PMQLONG Version, - PMQLONG pCompCode, - PMQLONG pReason) { - MQLONG CC = MQCC_OK; - MQLONG Reason = MQRC_NONE; - - if ((Options & MQZIO_PRIMARY) == MQZIO_PRIMARY) - primary_process = 1; - - fp=fopen("/var/mqm/errors/amqpasdev.log","a"); - - if (CC == MQCC_OK) - hc->MQZEP_Call(hc, MQZID_INIT_AUTHORITY,(PMQFUNC)PASStart,&CC,&Reason); - - if (CC == MQCC_OK) - hc->MQZEP_Call(hc,MQZID_TERM_AUTHORITY,(PMQFUNC)OATermAuth,&CC,&Reason); - - if (CC == MQCC_OK) - hc->MQZEP_Call(hc,MQZID_AUTHENTICATE_USER,(PMQFUNC)OAAuthUser,&CC,&Reason); - - if (CC == MQCC_OK) - hc->MQZEP_Call(hc,MQZID_FREE_USER,(PMQFUNC)OAFreeUser,&CC,&Reason); - - *Version = MQZAS_VERSION_5; - *pCompCode = CC; - *pReason = Reason; - - PrintDateTime(); - fprintf(fp, "Pluggable OAM Initialized.\n"); - fprintf(fp, "THIS IS A DEVELOPER ONLY CONFIGURATION AND NOT RECOMMENDED FOR PRODUCTION USAGE"); - return; -} - -static char *authuserfmt = - "\tUser : \"%12.12s\"\n"\ - "\tEffUser : \"%12.12s\"\n"\ - "\tAppName : \"%28.28s\"\n"\ - "\tApIdDt : \"%32.32s\"\n"\ - "\tEnv : \"%s\"\n"\ - "\tApp Pid : %d\n"\ - "\tApp Tid : %d\n"\ - ; -static void MQENTRY OAAuthUser ( - PMQCHAR pQMgrName, - PMQCSP pSecurityParms, - PMQZAC pApplicationContext, - PMQZIC pIdentityContext, - PMQPTR pCorrelationPtr, - PMQBYTE pComponentData, - PMQLONG pContinuation, - PMQLONG pCompCode, - PMQLONG pReason) -{ - char *spuser = NULL; - char *sppass = NULL; - int gorc = MQRC_NOT_AUTHORIZED; - - if ((pSecurityParms->CSPUserIdLength) > 0) { - //Grab the user creds from csp. - spuser = malloc(pSecurityParms->CSPUserIdLength+1); - strncpy(spuser,pSecurityParms->CSPUserIdPtr,pSecurityParms->CSPUserIdLength); - spuser[pSecurityParms->CSPUserIdLength]=0; - sppass = malloc(pSecurityParms->CSPPasswordLength+1); - strncpy(sppass,pSecurityParms->CSPPasswordPtr,pSecurityParms->CSPPasswordLength); - sppass[pSecurityParms->CSPPasswordLength]=0; - gorc = Authenticate(spuser,sppass); - - if (gorc == MQRC_NONE) { - *pCompCode = MQCC_OK; - *pReason = MQRC_NONE; - *pContinuation = MQZCI_CONTINUE; - memcpy( pIdentityContext->UserIdentifier - , spuser - , sizeof(pIdentityContext->UserIdentifier) ); - } else { - *pCompCode = MQCC_WARNING; - *pReason = MQRC_NONE; - *pContinuation = MQZCI_CONTINUE; - //we print to error file only if error'd - PrintDateTime(); - if (fp) { - fprintf(fp, authuserfmt, - pIdentityContext->UserIdentifier, - pApplicationContext->EffectiveUserID, - pApplicationContext->ApplName, - pIdentityContext->ApplIdentityData, - OAEnvStr(pApplicationContext->Environment), - pApplicationContext->ProcessId, - pApplicationContext->ThreadId); - - fprintf(fp,"\tCSP UserId : %s\n", spuser); - fprintf(fp,"\tCSP Password : %s\n", "****.."); - fprintf(fp,"\tPAS-Compcode:%d\n",*pCompCode); - fprintf(fp,"\tPAS-Reasoncode:%d\n",*pReason); - } - } - free(spuser); - free(sppass); - } else { - //this is only a normal UID authentication. - spuser = malloc(sizeof(PMQCHAR12)); - strncpy(spuser,pApplicationContext->EffectiveUserID,strlen(pApplicationContext->EffectiveUserID)); - spuser[sizeof(PMQCHAR12)]=0; - gorc = CheckAuthority(spuser); - if (gorc == MQRC_NONE){ - *pCompCode = MQCC_OK; - *pReason = MQRC_NONE; - *pContinuation = MQZCI_CONTINUE; - memcpy( pIdentityContext->UserIdentifier - , spuser - , sizeof(pIdentityContext->UserIdentifier) ); - } else { - *pCompCode = MQCC_WARNING; - *pReason = MQRC_NONE; - *pContinuation = MQZCI_CONTINUE; - //we print only if error'd - PrintDateTime(); - if (fp) - { - fprintf(fp, authuserfmt, - pIdentityContext->UserIdentifier, - pApplicationContext->EffectiveUserID, - pApplicationContext->ApplName, - pIdentityContext->ApplIdentityData, - OAEnvStr(pApplicationContext->Environment), - pApplicationContext->ProcessId, - pApplicationContext->ThreadId - ); - fprintf(fp,"\tUID : %s\n", spuser); - fprintf(fp,"\tPAS-Compcode:%d\n",*pCompCode); - fprintf(fp,"\tPAS-Reasoncode:%d\n",*pReason); - } - } - } - return; -} - -static void MQENTRY OAFreeUser ( - PMQCHAR pQMgrName, - PMQZFP pFreeParms, - PMQBYTE pComponentData, - PMQLONG pContinuation, - - PMQLONG pCompCode, - PMQLONG pReason) -{ - *pCompCode = MQCC_WARNING; - *pReason = MQRC_NONE; - *pContinuation = MQZCI_CONTINUE; - return; -} - -static void MQENTRY OATermAuth( - MQHCONFIG hc, - MQLONG Options, - PMQCHAR pQMgrName, - PMQBYTE pComponentData, - PMQLONG pCompCode, - PMQLONG pReason) -{ - if ((primary_process) && ((Options & MQZTO_PRIMARY) == MQZTO_PRIMARY) || - ((Options & MQZTO_SECONDARY) == MQZTO_SECONDARY)) - { - if (fp) - { - fclose(fp); - fp = NULL; - } - } - *pCompCode = MQCC_OK; - *pReason = MQRC_NONE; -} - -static void PrintDateTime() { - FindSize(); - struct tm *local; - time_t t; - t = time(NULL); - local = localtime(&t); - if (fp) { - fprintf(fp, "-------------------------------------------------\n"); - fprintf(fp, "Local time: %s", asctime(local)); - local = gmtime(&t); - fprintf(fp, "UTC time: %s", asctime(local)); - } - return; -} - -static char *OAEnvStr(MQLONG x) - { - switch (x) - { - case MQXE_OTHER: return "Application"; - case MQXE_MCA: return "Channel"; - case MQXE_MCA_SVRCONN: return "Channel SvrConn"; - case MQXE_COMMAND_SERVER: return "Command Server"; - case MQXE_MQSC: return "MQSC"; - default: return "Invalid Environment"; - } - } - -static void FindSize() -{ - int sz = 0; - int prev=ftell(fp); - fseek(fp, 0L, SEEK_END); - sz=ftell(fp); - //if log file size goes over 1mb, rewind it. - if (sz > 1000000) { - rewind(fp); - } else { - fseek(fp, prev, SEEK_SET); - } -} - -*/ -import "C" -import "github.com/ibm-messaging/mq-container/internal/htpasswd" - -//export MQStart -func MQStart(hc C.MQHCONFIG, Options C.MQLONG, QMgrName C.PMQCHAR, ComponentDataLength C.MQLONG, ComponentData C.PMQBYTE, Version C.PMQLONG, pCompCode C.PMQLONG, pReason C.PMQLONG) { - C.PASStart(hc, Options, QMgrName, ComponentDataLength, ComponentData, Version, pCompCode, pReason) -} - -//export Authenticate -func Authenticate(x *C.char, y *C.char) C.int { - user := C.GoString(x) - pwd := C.GoString(y) - found, ok, err := htpasswd.AuthenticateUser(user, pwd, false) - - if !found || !ok || err != nil { - return C.MQRC_UNKNOWN_OBJECT_NAME - } - return C.MQRC_NONE -} - -//export CheckAuthority -func CheckAuthority(x *C.char) C.int { - user := C.GoString(x) - found, err := htpasswd.ValidateUser(user, false) - if !found || err != nil { - return C.MQRC_UNKNOWN_OBJECT_NAME - } - return C.MQRC_NONE - -} - -func main() {} diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 25a21c7..d9463ff 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2018, 2019 +© 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. @@ -117,14 +117,22 @@ func (l *Logger) log(level string, msg string) { // Debug logs a line as debug func (l *Logger) Debug(args ...interface{}) { if l.debug { - l.log(debugLevel, fmt.Sprint(args...)) + 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 { - l.log(debugLevel, fmt.Sprintf(format, args...)) + if l.json { + l.log(debugLevel, fmt.Sprintf(format, args...)) + } else { + l.log(debugLevel, fmt.Sprintf("DEBUG: "+format, args...)) + } } } diff --git a/test/docker/devconfig_test.go b/test/docker/devconfig_test.go index e8aa2a0..5a53808 100644 --- a/test/docker/devconfig_test.go +++ b/test/docker/devconfig_test.go @@ -1,7 +1,7 @@ // +build mqdev /* -© Copyright IBM Corporation 2018, 2019 +© 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. @@ -43,6 +43,7 @@ func TestDevGoldenPath(t *testing.T) { Env: []string{ "LICENSE=accept", "MQ_QMGR_NAME=" + qm, + "DEBUG=true", }, } id := runContainerWithPorts(t, cli, &containerConfig, []int{9443}) @@ -82,6 +83,8 @@ func TestDevSecure(t *testing.T) { "MQ_QMGR_NAME=" + qm, "MQ_APP_PASSWORD=" + appPassword, "DEBUG=1", + "WLP_LOGGING_MESSAGE_FORMAT=JSON", + "MQ_ENABLE_EMBEDDED_WEB_SERVER_LOG=true", }, Image: imageName(), } diff --git a/test/docker/devconfig_test_util.go b/test/docker/devconfig_test_util.go index 6b13579..45d5a61 100644 --- a/test/docker/devconfig_test_util.go +++ b/test/docker/devconfig_test_util.go @@ -1,7 +1,7 @@ // +build mqdev /* -© Copyright IBM Corporation 2018, 2019 +© 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. @@ -48,8 +48,9 @@ var insecureTLSConfig *tls.Config = &tls.Config{ } func waitForWebReady(t *testing.T, cli *client.Client, ID string, tlsConfig *tls.Config) { + t.Logf("%s Waiting for web server to be ready", time.Now().Format(time.RFC3339)) httpClient := http.Client{ - Timeout: time.Duration(3 * time.Second), + Timeout: time.Duration(10 * time.Second), Transport: &http.Transport{ TLSClientConfig: tlsConfig, }, @@ -63,13 +64,13 @@ func waitForWebReady(t *testing.T, cli *client.Client, ID string, tlsConfig *tls case <-time.After(1 * time.Second): req, err := http.NewRequest("GET", url, nil) req.SetBasicAuth("admin", defaultAdminPassword) - resp, err := httpClient.Do(req.WithContext(ctx)) + resp, err := httpClient.Do(req) if err == nil && resp.StatusCode == http.StatusOK { - t.Log("MQ web server is ready") + t.Logf("%s MQ web server is ready", time.Now().Format(time.RFC3339)) return } case <-ctx.Done(): - t.Fatal("Timed out waiting for web server to become ready") + t.Fatalf("%s Timed out waiting for web server to become ready", time.Now().Format(time.RFC3339)) } } } diff --git a/test/messaging/Dockerfile b/test/messaging/Dockerfile index 4dc87fc..92d78c1 100644 --- a/test/messaging/Dockerfile +++ b/test/messaging/Dockerfile @@ -1,4 +1,4 @@ -# © Copyright IBM Corporation 2018, 2019 +# © 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. @@ -33,4 +33,5 @@ RUN find /usr/src/mymaven FROM docker.io/ibmjava:8-jre COPY --from=builder /usr/src/mymaven/target/*.jar /opt/app/ COPY --from=builder /usr/src/mymaven/target/lib/*.jar /opt/app/ +USER 1001 ENTRYPOINT ["java", "-classpath", "/opt/app/*", "org.junit.platform.console.ConsoleLauncher", "-p", "com.ibm.mqcontainer.test", "--details", "verbose"] diff --git a/test/messaging/pom.xml b/test/messaging/pom.xml index bbdd556..55232f4 100644 --- a/test/messaging/pom.xml +++ b/test/messaging/pom.xml @@ -1,5 +1,5 @@