-
Notifications
You must be signed in to change notification settings - Fork 750
feat(host): auto-generate certificates for host mode #7362
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
Changes from all commits
Commits
Show all changes
4 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
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
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,131 @@ | ||
| // Copyright Envoy Gateway Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // The full text of the Apache license is available in the LICENSE file at | ||
| // the root of the repo. | ||
|
|
||
| package host | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/envoyproxy/gateway/internal/envoygateway/config" | ||
| "github.com/envoyproxy/gateway/internal/infrastructure/common" | ||
| "github.com/envoyproxy/gateway/internal/utils/file" | ||
| ) | ||
|
|
||
| func TestMaybeGenerateCertificates(t *testing.T) { | ||
| cfg, err := config.New(io.Discard, io.Discard) | ||
| require.NoError(t, err) | ||
|
|
||
| certFiles := []string{"ca.crt", "tls.crt", "tls.key"} | ||
|
|
||
| t.Run("all_files_exist", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| certPath := filepath.Join(tmpDir, "envoy") | ||
|
|
||
| // Create directory and dummy files | ||
| require.NoError(t, os.MkdirAll(certPath, 0o750)) | ||
| for _, filename := range certFiles { | ||
| fpath := filepath.Join(certPath, filename) | ||
| require.NoError(t, os.WriteFile(fpath, []byte("dummy"), 0o600)) | ||
| } | ||
|
|
||
| err := maybeGenerateCertificates(cfg, certPath) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify files still exist and unchanged size | ||
| for _, filename := range certFiles { | ||
| data, err := os.ReadFile(filepath.Join(certPath, filename)) | ||
| require.NoError(t, err) | ||
| require.Len(t, data, 5) // "dummy" | ||
| } | ||
| }) | ||
|
|
||
| t.Run("missing_files", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| certPath := filepath.Join(tmpDir, "envoy") | ||
|
|
||
| err := maybeGenerateCertificates(cfg, certPath) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify directory created | ||
| info, err := os.Stat(certPath) | ||
| require.NoError(t, err) | ||
| require.True(t, info.IsDir()) | ||
|
|
||
| // Verify all files created and non-empty | ||
| for _, filename := range certFiles { | ||
| data, err := os.ReadFile(filepath.Join(certPath, filename)) | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, data, filename) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("partial_files_missing", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| certPath := filepath.Join(tmpDir, "envoy") | ||
|
|
||
| require.NoError(t, os.MkdirAll(certPath, 0o750)) | ||
|
|
||
| // Create only one file | ||
| require.NoError(t, os.WriteFile(filepath.Join(certPath, "ca.crt"), []byte("dummy"), 0o600)) | ||
|
|
||
| err := maybeGenerateCertificates(cfg, certPath) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify all files created and non-empty | ||
| for _, filename := range certFiles { | ||
| data, err := os.ReadFile(filepath.Join(certPath, filename)) | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, data, filename) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("cert_generation_fails", func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| // This tests mkdir fail by making parent unwritable | ||
| unwritableDir := filepath.Join(tmpDir, "unwritable") | ||
| require.NoError(t, os.Mkdir(unwritableDir, 0o555)) // Read-only | ||
|
|
||
| badCertPath := filepath.Join(unwritableDir, "envoy") | ||
| err := maybeGenerateCertificates(cfg, badCertPath) | ||
| require.ErrorContains(t, err, "failed to create cert directory") | ||
| }) | ||
| } | ||
|
|
||
| func TestCreateSdsConfig(t *testing.T) { | ||
| t.Run("success", func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| // Create required cert files | ||
| require.NoError(t, file.Write("test ca", filepath.Join(dir, XdsTLSCaFilename))) | ||
| require.NoError(t, file.Write("test cert", filepath.Join(dir, XdsTLSCertFilename))) | ||
| require.NoError(t, file.Write("test key", filepath.Join(dir, XdsTLSKeyFilename))) | ||
|
|
||
| err := createSdsConfig(dir) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify CA config was created | ||
| caConfigPath := filepath.Join(dir, common.SdsCAFilename) | ||
| actualCAConfig, err := os.ReadFile(caConfigPath) | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, actualCAConfig) | ||
|
|
||
| // Verify cert config was created | ||
| certConfigPath := filepath.Join(dir, common.SdsCertFilename) | ||
| actualCertConfig, err := os.ReadFile(certConfigPath) | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, actualCertConfig) | ||
| }) | ||
|
|
||
| t.Run("error_writing_ca_config", func(t *testing.T) { | ||
| // Use invalid path to force file.Write to fail | ||
| invalidDir := filepath.Join("/", "nonexistent", "invalid", "path") | ||
| err := createSdsConfig(invalidDir) | ||
| require.Error(t, err) | ||
| }) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might be wrong -- paths.CertDir("") instead of paths.CertDir("envoy"). let me confirm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure on how to fix but envoyproxy/ai-gateway#1470 tests are failing without something