Skip to content

cmd/tls: set explicit file permissions for generated certs #22286

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
merged 5 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .changelog/22286.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:security
cli: update tls ca and cert create to reduce excessive file perms for generated public files
```
61 changes: 32 additions & 29 deletions .github/workflows/test-integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -452,35 +452,38 @@ jobs:
cat /etc/hosts && echo "-----------"
sudo sed -i 's/::1 *localhost ip6-localhost ip6-loopback/::1 ip6-localhost ip6-loopback/g' /etc/hosts
cat /etc/hosts
- name: Compatibility Integration Tests
run: |
mkdir -p "/tmp/test-results"
cd ./test/integration/consul-container
docker run --rm ${{ env.CONSUL_LATEST_IMAGE_NAME }}:local consul version
go run gotest.tools/gotestsum@v${{env.GOTESTSUM_VERSION}} \
--raw-command \
--format=github-actions \
--rerun-fails \
-- \
go test \
-p=6 \
-parallel=4 \
-tags "${{ env.GOTAGS }}" \
-timeout=30m \
-json \
`go list -tags "${{ env.GOTAGS }}" ./... | grep -v upgrade | grep -v peering_commontopo` \
--target-image ${{ env.CONSUL_LATEST_IMAGE_NAME }} \
--target-version local \
--latest-image docker.mirror.hashicorp.services/${{ env.CONSUL_LATEST_IMAGE_NAME }} \
--latest-version latest
ls -lrt
env:
# this is needed because of incompatibility between RYUK container and GHA
GOTESTSUM_JUNITFILE: ${{ env.TEST_RESULTS_DIR }}/results.xml
GOTESTSUM_FORMAT: standard-verbose
COMPOSE_INTERACTIVE_NO_CLI: 1
# tput complains if this isn't set to something.
TERM: ansi

# TODO: Disabling the "Compatibility Integration Tests" test temporarily, need to enable again once dependent pipeline issues are fixed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the pipeline broken from the changes or was it broken earlier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's an existing issue. This issue is being tracked internally here.

# Please run this test locally and post results in PR description.
# - name: Compatibility Integration Tests
# run: |
# mkdir -p "/tmp/test-results"
# cd ./test/integration/consul-container
# docker run --rm ${{ env.CONSUL_LATEST_IMAGE_NAME }}:local consul version
# go run gotest.tools/gotestsum@v${{env.GOTESTSUM_VERSION}} \
# --raw-command \
# --format=github-actions \
# --rerun-fails \
# -- \
# go test \
# -p=6 \
# -parallel=4 \
# -tags "${{ env.GOTAGS }}" \
# -timeout=30m \
# -json \
# `go list -tags "${{ env.GOTAGS }}" ./... | grep -v upgrade | grep -v peering_commontopo` \
# --target-image ${{ env.CONSUL_LATEST_IMAGE_NAME }} \
# --target-version local \
# --latest-image docker.mirror.hashicorp.services/${{ env.CONSUL_LATEST_IMAGE_NAME }} \
# --latest-version latest
# ls -lrt
# env:
# # this is needed because of incompatibility between RYUK container and GHA
# GOTESTSUM_JUNITFILE: ${{ env.TEST_RESULTS_DIR }}/results.xml
# GOTESTSUM_FORMAT: standard-verbose
# COMPOSE_INTERACTIVE_NO_CLI: 1
# # tput complains if this isn't set to something.
# TERM: ansi

# NOTE: ENT specific step as we store secrets in Vault.
- name: Authenticate to Vault
Expand Down
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.23.7
1.23.8
15 changes: 13 additions & 2 deletions command/tls/ca/create/tls_ca_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ func New(ui cli.Ui) *cmd {
return c
}

const (
// DirectoryPerms represents read+write+execute for owner, read+execute for group and others (0755)
DirectoryPerms = 0755
// PublicFilePerms represents read+write for owner, read-only for group and others (0644)
PublicFilePerms = 0644
// PrivateFilePerms represents read+write for owner only (0600)
PrivateFilePerms = 0600
)

type cmd struct {
UI cli.Ui
flags *flag.FlagSet
Expand Down Expand Up @@ -82,13 +91,15 @@ func (c *cmd) Run(args []string) int {
return 1
}

if err := file.WriteAtomicWithPerms(certFileName, []byte(ca), 0755, 0666); err != nil {
// public CA cert file
if err := file.WriteAtomicWithPerms(certFileName, []byte(ca), DirectoryPerms, PublicFilePerms); err != nil {
c.UI.Error(err.Error())
return 1
}
c.UI.Output("==> Saved " + certFileName)

if err := file.WriteAtomicWithPerms(pkFileName, []byte(pk), 0755, 0600); err != nil {
// CA private key
if err := file.WriteAtomicWithPerms(pkFileName, []byte(pk), DirectoryPerms, PrivateFilePerms); err != nil {
c.UI.Error(err.Error())
return 1
}
Expand Down
15 changes: 13 additions & 2 deletions command/tls/cert/create/tls_cert_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func New(ui cli.Ui) *cmd {
return c
}

const (
// DirectoryPerms represents read+write+execute for owner, read+execute for group and others (0755)
DirectoryPerms = 0755
// PublicFilePerms represents read+write for owner, read-only for group and others (0644)
PublicFilePerms = 0644
// PrivateFilePerms represents read+write for owner only (0600)
PrivateFilePerms = 0600
)

type cmd struct {
UI cli.Ui
flags *flag.FlagSet
Expand Down Expand Up @@ -193,13 +202,15 @@ func (c *cmd) Run(args []string) int {
return 1
}

if err := file.WriteAtomicWithPerms(certFileName, []byte(pub), 0755, 0666); err != nil {
// public cert
if err := file.WriteAtomicWithPerms(certFileName, []byte(pub), DirectoryPerms, PublicFilePerms); err != nil {
c.UI.Error(err.Error())
return 1
}
c.UI.Output("==> Saved " + certFileName)

if err := file.WriteAtomicWithPerms(pkFileName, []byte(priv), 0755, 0600); err != nil {
// private key
if err := file.WriteAtomicWithPerms(pkFileName, []byte(priv), DirectoryPerms, PrivateFilePerms); err != nil {
c.UI.Error(err.Error())
return 1
}
Expand Down
Loading