-
Notifications
You must be signed in to change notification settings - Fork 5
Fix PostgreSQL credential security and extract random string generation #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-bot
merged 2 commits into
openstack-k8s-operators:main
from
lpiwowar:lpiwowar/postgresql-password
Jul 13, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,25 +1,22 @@ | ||
| #!/bin/bash | ||
|
|
||
| # This script prepares databases for lightspeed-stack and llama-stack (OGX) with | ||
| # postgres_bootstrap.sql. | ||
| # | ||
| # Note: | ||
| # - lightspeed-stack database: Auto-created by container image via POSTGRESQL_DATABASE. | ||
| # - llama-stack database: Explicitly created by this script via POSTGRESQL_LLAMA_STACK_DATABASE. | ||
| # - POSTGRESQL_ADMIN_PASSWORD is intentionally not set. The postgres superuser has no password | ||
| # by default, which restricts it to local connections only — a deliberate security improvement. | ||
| # Setting POSTGRESQL_ADMIN_PASSWORD would enable remote login for the postgres account. | ||
| set -e | ||
|
|
||
| cat /var/lib/pgsql/data/userdata/postgresql.conf | ||
|
|
||
| echo "attempting to create llama-stack database and pg_trgm extension if they do not exist" | ||
|
|
||
| _psql () { psql --set ON_ERROR_STOP=1 "$@" ; } | ||
|
|
||
| # Create database for llama-stack conversation storage | ||
| DB_NAME="llamastack" | ||
|
|
||
| echo "SELECT 'CREATE DATABASE $DB_NAME' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$DB_NAME')\gexec" | _psql -d "$POSTGRESQL_DATABASE" | ||
|
|
||
| # Create pg_trgm extension in default database (for OpenStack Lightspeed conversation cache) | ||
| echo "CREATE EXTENSION IF NOT EXISTS pg_trgm;" | _psql -d "$POSTGRESQL_DATABASE" | ||
|
|
||
| # Create pg_trgm extension in llama-stack database (for text search if needed) | ||
| echo "CREATE EXTENSION IF NOT EXISTS pg_trgm;" | _psql -d $DB_NAME | ||
| echo "Bootstrapping PostgreSQL databases and permissions" | ||
|
|
||
| # Create schemas for isolating different components' data | ||
| echo "CREATE SCHEMA IF NOT EXISTS lcore;" | _psql -d "$POSTGRESQL_DATABASE" | ||
| echo "CREATE SCHEMA IF NOT EXISTS quota;" | _psql -d "$POSTGRESQL_DATABASE" | ||
| echo "CREATE SCHEMA IF NOT EXISTS conversation_cache;" | _psql -d "$POSTGRESQL_DATABASE" | ||
| psql \ | ||
| -v ON_ERROR_STOP=1 \ | ||
| -v postgresql_user="$POSTGRESQL_USER" \ | ||
| -v postgresql_lightspeed_stack_database="$POSTGRESQL_DATABASE" \ | ||
| -v postgresql_llama_stack_database="$POSTGRESQL_LLAMA_STACK_DATABASE" \ | ||
| -f "$POSTGRESQL_BOOTSTRAP_SQL_FILE" | ||
This file contains hidden or 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,29 @@ | ||
| -- 1) LIGHTSPEED STACK DATABASE CONFIGURATION --------------------------------- | ||
| \c :postgresql_lightspeed_stack_database | ||
|
|
||
| -- PostgreSQL 15+ removed the default CREATE/USAGE grants on the public schema. | ||
| -- Therefore we have to explicitly grant the permissions to the user. | ||
| GRANT USAGE, CREATE ON SCHEMA public TO :"postgresql_user"; | ||
|
|
||
| -- lightspeed-stack requires rights to create additional schemas under its database | ||
| GRANT CREATE ON DATABASE :"postgresql_lightspeed_stack_database" TO :"postgresql_user"; | ||
|
|
||
| -- pg_trgm is the trigram similarity extension for PostgreSQL (enables e.g. fuzzy text search) | ||
| CREATE EXTENSION IF NOT EXISTS pg_trgm; | ||
| ------------------------------------------------------------------------------- | ||
|
|
||
| -- 2) LLAMA STACK DATABASE CONFIGURATION -------------------------------------- | ||
| -- Create postgresql_llama_stack_database. | ||
| SELECT format('CREATE DATABASE %I', :'postgresql_llama_stack_database') | ||
| WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = :'postgresql_llama_stack_database')\gexec | ||
|
|
||
| -- Connect and configure postgresql_llama_stack_database | ||
| \c :postgresql_llama_stack_database | ||
|
|
||
| -- PostgreSQL 15+ removed the default CREATE/USAGE grants on the public schema. | ||
| -- Therefore we have to explicitly grant the permissions to the user. | ||
| GRANT USAGE, CREATE ON SCHEMA public TO :"postgresql_user"; | ||
|
|
||
| -- pg_trgm is the trigram similarity extension for PostgreSQL (enables e.g. fuzzy text search) | ||
| CREATE EXTENSION IF NOT EXISTS pg_trgm; | ||
| ------------------------------------------------------------------------------- |
This file contains hidden or 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 hidden or 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,100 @@ | ||
| /* | ||
| Copyright 2026. | ||
|
|
||
| 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 controller | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGenerateRandomStringLength(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| length int | ||
| }{ | ||
| { | ||
| name: "Zero length", | ||
| length: 0, | ||
| }, | ||
| { | ||
| name: "Odd length 1", | ||
| length: 1, | ||
| }, | ||
| { | ||
| name: "Even length 2", | ||
| length: 2, | ||
| }, | ||
| { | ||
| name: "Odd length 7", | ||
| length: 7, | ||
| }, | ||
| { | ||
| name: "Even length 8", | ||
| length: 8, | ||
| }, | ||
| { | ||
| name: "Odd length 15", | ||
| length: 15, | ||
| }, | ||
| { | ||
| name: "Even length 16", | ||
| length: 16, | ||
| }, | ||
| { | ||
| name: "Even length 32", | ||
| length: 32, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result, err := generateRandomString(tt.length) | ||
| if err != nil { | ||
| t.Errorf("generateRandomString(%d) unexpected error: %v", tt.length, err) | ||
| } | ||
| if len(result) != tt.length { | ||
| t.Errorf("generateRandomString(%d) returned length %d, want %d", tt.length, len(result), tt.length) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGenerateRandomStringHexCharacters(t *testing.T) { | ||
| result, err := generateRandomString(32) | ||
| if err != nil { | ||
| t.Fatalf("generateRandomString(32) unexpected error: %v", err) | ||
| } | ||
| for i, c := range result { | ||
| if (c < '0' || c > '9') && (c < 'a' || c > 'f') { | ||
| t.Errorf("generateRandomString(32) character at index %d is %q, not a lowercase hex character", i, c) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestGenerateRandomStringUniqueness(t *testing.T) { | ||
| const length = 16 | ||
| a, err := generateRandomString(length) | ||
| if err != nil { | ||
| t.Fatalf("first call unexpected error: %v", err) | ||
| } | ||
| b, err := generateRandomString(length) | ||
| if err != nil { | ||
| t.Fatalf("second call unexpected error: %v", err) | ||
| } | ||
| if a == b { | ||
| t.Errorf("generateRandomString(%d) returned identical values across two calls: %q", length, a) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.