Skip to content
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

feat(core): adds storeFile to save encrypted profiles to disk and updates auth to propagate tlsNoVerify #420

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ab8a001
Adds storeFile to save encrypted profiles to disk and updates auth to…
jgilpin Nov 6, 2024
6c79fb6
Don't add the profile name to the namespace, results in duplicate.
jgilpin Nov 6, 2024
20c7ea2
Include version in metadata/profile
jgilpin Nov 6, 2024
dd37e3b
feat(profiles): improve UTC consistency, update file handling, and re…
jgilpin Nov 11, 2024
67f1188
Adds storeFile to save encrypted profiles to disk and updates auth to…
jgilpin Nov 6, 2024
87ee44e
Don't add the profile name to the namespace, results in duplicate.
jgilpin Nov 6, 2024
13e5879
Include version in metadata/profile
jgilpin Nov 6, 2024
a0979b9
feat(profiles): improve UTC consistency, update file handling, and re…
jgilpin Nov 11, 2024
abba51d
Merge branch 'feature/update-profiles-and-auth-for-windows' of https:…
jgilpin Nov 11, 2024
8802b6c
Merge branch 'main' of github.com:opentdf/otdfctl into feature/update…
jrschumacher Nov 12, 2024
e6e0858
Allow for make build to use -j switch for multi threaded build
jgilpin Nov 15, 2024
e33a701
Update opentdf deps
jgilpin Nov 15, 2024
7daced2
Use config.AppName to have unique profiles across different apps
jgilpin Nov 15, 2024
2454afe
defaulting to https if the endpoint is missing a scheme is ok
jgilpin Nov 15, 2024
bc83061
Update pkg/profiles/storeFile.go
jgilpin Nov 15, 2024
f7d93f9
Update docs/man/_index.md
jgilpin Nov 15, 2024
aa1d20d
Update pkg/auth/auth.go
jgilpin Nov 15, 2024
5fd9ea6
Update pkg/profiles/storeFile.go
jgilpin Nov 15, 2024
7828a43
Update pkg/profiles/storeFile.go
jgilpin Nov 15, 2024
bb6dc72
Update pkg/profiles/storeFile.go
jgilpin Nov 15, 2024
d104fad
Update pkg/profiles/storeFile.go
jgilpin Nov 15, 2024
cbb1ac4
Update pkg/profiles/storeFile.go
jgilpin Nov 15, 2024
d86f0e0
Merge branch 'main' into feature/update-profiles-and-auth-for-windows
jgilpin Nov 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 50 additions & 22 deletions .github/scripts/verify-checksums.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,60 @@

# Check if the required arguments are provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <outputDir> <checksumFile>"
echo "Usage: $0 <output_directory> <checksum_file>"
exit 1
fi

echo "Verifying checksums..."
# Location of the checksum file
checksumFile=$1/$2
outputDir=$1
# Assign arguments to variables
output_dir="$1"
checksum_file="$2"
checksum_path="${output_dir}/${checksum_file}" # Full path to the checksum file
lock_file="${checksum_path}.lock" # Append .lock to the full path of the checksum file

# Ensure the checksum file exists
if [ ! -f "$checksum_path" ]; then
echo "ERROR: Checksum file $checksum_path does not exist."
exit 1
fi

echo "Looking for checksum file: $checksumFile"
test -f "$checksumFile" || { echo "ERROR: Checksum file not found!"; exit 1; }
# Wait for the lock file to be available for reading
exec 200<"$lock_file" # Open lock file descriptor for reading
flock -s 200 # Acquire shared lock (will wait if exclusive lock is held)

echo "Verifying checksums..."
echo "Looking for checksum file: $checksum_path"

# Iterate over each line in the checksum file
while read -r line; do
# Extract the expected checksum and filename from each line
read -ra ADDR <<< "$line" # Read the line into an array
expectedChecksum="${ADDR[0]}"
fileName="${ADDR[2]}"

# Calculate the actual checksum of the file
actualChecksum=$(shasum -a 256 "$outputDir/$fileName" | awk '{print $1}')

# Compare the expected checksum with the actual checksum
if [ "$expectedChecksum" == "$actualChecksum" ]; then
echo "SUCCESS: Checksum for $fileName is valid."
else
echo "ERROR: Checksum for $fileName does not match."
fi
done < "$checksumFile"
# Extract checksum and filename from the line
expected_checksum=$(echo "$line" | awk '{print $1}')
filename=$(echo "$line" | awk '{print $2}')

# Construct the full path to the file
file_path="$output_dir/$filename"

# Check if the file exists
if [ ! -f "$file_path" ]; then
echo "ERROR: File $filename not found in $output_dir"
continue
fi

# Calculate the actual checksum of the file
actual_checksum=$(shasum -a 256 "$file_path" | awk '{print $1}')

# Compare the expected and actual checksums
if [ "$expected_checksum" != "$actual_checksum" ]; then
echo "ERROR: Checksum for $filename does not match."
else
echo "Checksum for $filename is correct."
fi
done < "$checksum_path"

# Release the lock and close the lock file descriptor
flock -u 200
exec 200>&-

# Clean up the lock file
rm -f "$lock_file"

echo "Checksum verification completed."
52 changes: 35 additions & 17 deletions .github/scripts/zip-builds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,41 @@ mkdir -p "$output_dir"

# Create a checksums file
checksums_file="$output_dir/${build_semver}_checksums.txt"
touch $checksums_file
touch "$checksums_file"

# Define a lock file for parallel-safe writing to checksums
checksums_lockfile="${checksums_file}.lock"

# Iterate over each binary file
for binary_file in "$binary_dir"/*; do
compressed=""
if [[ $binary_file == *.exe ]]; then
# If the file is a Windows binary, zip it
filename=$(basename "$binary_file")
compressed="${filename%.exe}.zip"
zip -j "$output_dir/$compressed" "$binary_file"
else
# For other binaries, tar and gzip them
filename=$(basename "$binary_file")
compressed="${filename}.tar.gz"
tar -czf "$output_dir/$compressed" "$binary_file"
fi

# Append checksums to the file
echo "$(cat "$output_dir/$compressed" | shasum -a 256) $compressed" >> $checksums_file
done
(
compressed=""
if [[ $binary_file == *.exe ]]; then
# If the file is a Windows binary, zip it
filename=$(basename "$binary_file")
compressed="${filename%.exe}.zip"
zip -j "$output_dir/$compressed" "$binary_file"
else
# For other binaries, tar and gzip them
filename=$(basename "$binary_file")
compressed="${filename}.tar.gz"
tar -czf "$output_dir/$compressed" "$binary_file"
fi

# Compute checksum and append it to the checksums file using a lock
checksum="$(shasum -a 256 "$output_dir/$compressed" | awk '{print $1}')"
(
flock -x 200
echo "$checksum $compressed" >> "$checksums_file"
) 200>"$checksums_lockfile"

) &
done

# Echo message indicating background tasks are running
echo "All zip and tar processes started. Waiting for them to finish..."

# Wait for all background processes to complete
wait

echo "All compression and checksum operations completed."
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ build-%:
go build $(GO_BUILD_FLAGS) \
-o $(GO_BUILD_PREFIX)-$(word 1,$(subst -, ,$*))-$(word 2,$(subst -, ,$*))$(word 3,$(subst -, ,$*))

zip-builds:
zip-builds: $(addprefix build-,$(PLATFORMS))
./.github/scripts/zip-builds.sh $(BINARY_NAME)-$(CURR_VERSION) $(TARGET_DIR) $(OUTPUT_DIR)

verify-checksums:
verify-checksums: zip-builds
./.github/scripts/verify-checksums.sh $(OUTPUT_DIR) $(BINARY_NAME)-$(CURR_VERSION)_checksums.txt

# Target for running the project (adjust as necessary for your project)
Expand Down Expand Up @@ -93,3 +93,4 @@ test-bats: build-test
.PHONY: clean
clean:
rm -rf $(TARGET_DIR)
rm -rf $(OUTPUT_DIR)
9 changes: 7 additions & 2 deletions cmd/auth-login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ func auth_codeLogin(cmd *cobra.Command, args []string) {
_, cp := InitProfile(c, false)

c.Print("Initiating login...")

// Use profile values as defaults, with command-line overrides
tlsNoVerify := c.FlagHelper.GetOptionalBoolWithDefault("tls-no-verify", cp.GetTLSNoVerify())
clientId := c.FlagHelper.GetOptionalStringWithDefault("client-id", cp.GetAuthCredentials().ClientId)

tok, publicClientID, err := auth.LoginWithPKCE(
cmd.Context(),
cp.GetEndpoint(),
c.FlagHelper.GetOptionalString("client-id"),
c.FlagHelper.GetOptionalBool("tls-no-verify"),
clientId,
tlsNoVerify,
)
if err != nil {
c.Println("failed")
Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,11 @@ func init() {
rootCmd.GetDocFlag("with-access-token").Default,
rootCmd.GetDocFlag("with-access-token").Description,
)

RootCmd.PersistentFlags().String(
rootCmd.GetDocFlag("profile-driver").Name,
rootCmd.GetDocFlag("profile-driver").Default,
rootCmd.GetDocFlag("profile-driver").Description,
)
RootCmd.AddGroup(&cobra.Group{ID: TDF})
}
7 changes: 7 additions & 0 deletions docs/man/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ command:
- name: debug
description: enable debug output
default: false
- name: profile-driver
description: storage driver for managing profiles [keyring, in-memory, file]
jgilpin marked this conversation as resolved.
Show resolved Hide resolved
enum:
- keyring
- in-memory
- file
default: file
---
15 changes: 12 additions & 3 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const (

// Facilitates an auth code PKCE flow to obtain OIDC tokens.
// Spawns a local server to handle the callback and opens a browser window in each respective OS.
func Login(ctx context.Context, platformEndpoint, tokenURL, authURL, publicClientID string) (*oauth2.Token, error) {
func Login(ctx context.Context, platformEndpoint, tokenURL, authURL, publicClientID string, tlsNoVerify bool) (*oauth2.Token, error) {
// Generate random hash and encryption keys for cookie handling
hashKey := make([]byte, keyLength)
encryptKey := make([]byte, keyLength)
Expand All @@ -239,9 +239,18 @@ func Login(ctx context.Context, platformEndpoint, tokenURL, authURL, publicClien
},
}

cookiehandler := httphelper.NewCookieHandler(hashKey, encryptKey)
cookiehandler := httphelper.NewCookieHandler(hashKey, encryptKey,
func() httphelper.CookieHandlerOpt {
if tlsNoVerify {
return httphelper.WithUnsecure()
}
return func(c *httphelper.CookieHandler) {} // No-op function if tlsNoVerify is false
}(),
)
jgilpin marked this conversation as resolved.
Show resolved Hide resolved

relyingParty, err := oidcrp.NewRelyingPartyOAuth(conf,
// respect tlsNoVerify
oidcrp.WithHTTPClient(utils.NewHttpClient(tlsNoVerify)),
// allow cookie handling for PKCE
oidcrp.WithCookieHandler(cookiehandler),
// use PKCE
Expand Down Expand Up @@ -271,7 +280,7 @@ func LoginWithPKCE(ctx context.Context, host, publicClientID string, tlsNoVerify
return nil, "", fmt.Errorf("failed to get platform configuration: %w", err)
}

tok, err := Login(ctx, host, pc.tokenEndpoint, pc.authzEndpoint, pc.publicClientID)
tok, err := Login(ctx, host, pc.tokenEndpoint, pc.authzEndpoint, pc.publicClientID, tlsNoVerify)
if err != nil {
return nil, "", fmt.Errorf("failed to login: %w", err)
}
Expand Down
29 changes: 23 additions & 6 deletions pkg/cli/flagValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ func (f flagHelper) GetOptionalID(idFlag string) string {
}

func (f flagHelper) GetOptionalString(flag string) string {
p := f.cmd.Flag(flag)
if p == nil {
return ""
return f.GetOptionalStringWithDefault(flag, "")
}

// GetOptionalStringWithDefault retrieves a string flag, or returns the default value if the flag is not set
func (f flagHelper) GetOptionalStringWithDefault(flagName string, defaultValue string) string {
if f.cmd.Flags().Changed(flagName) {
value, err := f.cmd.Flags().GetString(flagName)
if err == nil {
return value
}
}
return p.Value.String()
return defaultValue
}

func (f flagHelper) GetStringSlice(flag string, v []string, opts FlagsStringSliceOptions) []string {
Expand All @@ -82,8 +89,18 @@ func (f flagHelper) GetRequiredInt32(flag string) int32 {
}

func (f flagHelper) GetOptionalBool(flag string) bool {
v, _ := f.cmd.Flags().GetBool(flag)
return v
return f.GetOptionalBoolWithDefault(flag, false)
}

// GetOptionalBoolWithDefault retrieves a boolean flag, or returns the default value if the flag is not set
func (f flagHelper) GetOptionalBoolWithDefault(flagName string, defaultValue bool) bool {
if f.cmd.Flags().Changed(flagName) {
value, err := f.cmd.Flags().GetBool(flagName)
if err == nil {
return value
}
}
return defaultValue
}

func (f flagHelper) GetRequiredBool(flag string) bool {
Expand Down
Loading
Loading