Skip to content

Commit

Permalink
fix permissions for merged config
Browse files Browse the repository at this point in the history
  • Loading branch information
fqjony committed Nov 28, 2024
1 parent 8d3b2a6 commit 8539c61
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ RUN groupadd -g ${GID} ${USER} && \

# Prepare directories for the user and worker configuration
RUN mkdir -p /etc/worker /home/${USER}/.cd/bin /home/${USER}/.cd/configs && \
chown -R ${UID}:${GID} /etc/worker /home/${USER}/.cd
touch /home/${USER}/.cd/configs/merged_worker.yml && \
chown -R ${UID}:${GID} /etc/worker /home/${USER}/.cd && \
chmod 600 /home/${USER}/.cd/configs/merged_worker.yml

# Switch to the user directory
WORKDIR /home/${USER}
Expand Down
32 changes: 10 additions & 22 deletions lib/worker_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fi
# Ensure configuration file exists
ensure_config_exists() {
local config_path="$1"
if [[ ! -f "$config_path" ]]; then
log_error "Configuration file not found: $config_path"
if [[ ! -s "$config_path" ]]; then
log_error "Configuration file not found or empty: $config_path"
return 1
fi
}
Expand All @@ -36,39 +36,36 @@ merge_worker_configs() {
# Ensure built-in config exists
ensure_config_exists "$BUILT_IN_CONFIG" || return 1

# Ensure target directory exists
mkdir -p "$(dirname "$MERGED_CONFIG")"

# If a user-provided configuration exists, merge it
if [[ -f "$USER_CONFIG" ]]; then
log_info "User configuration detected. Merging with the built-in configuration."

# Merge configurations, prioritizing user-provided values
if ! yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' "$BUILT_IN_CONFIG" "$USER_CONFIG" > "$MERGED_CONFIG"; then
log_error "Failed to merge configurations. yq returned an error."
return 1
fi
else
log_info "No user configuration provided. Using built-in configuration only."
cp "$BUILT_IN_CONFIG" "$MERGED_CONFIG"
fi

log_info "Merged configuration created successfully at $MERGED_CONFIG"
# Copy the built-in configuration to the merged configuration
if ! cp "$BUILT_IN_CONFIG" "$MERGED_CONFIG"; then
log_error "Failed to copy built-in configuration to merged configuration."
return 1
fi
fi
}

# Load and parse the merged configuration
load_and_parse_config() {
merge_worker_configs || return 1

# Suppress logs when parsing YAML into JSON
# Parse the merged configuration into JSON
local json_output
if ! json_output=$(yq eval -o=json "$MERGED_CONFIG" 2>/dev/null); then
log_error "Failed to parse merged YAML from $MERGED_CONFIG. yq returned an error."
return 1
fi

# Ensure output is valid JSON
validate_json "$json_output" || return 1

echo "$json_output"
}

Expand All @@ -90,12 +87,3 @@ get_config_section() {

echo "$extracted_section"
}

# Debugging helper: Validate JSON structure
validate_json() {
local json="$1"
if ! echo "$json" | jq empty 2>/dev/null; then
log_error "Invalid JSON structure detected."
return 1
fi
}

0 comments on commit 8539c61

Please sign in to comment.