Skip to content

Commit

Permalink
Show error if there's no secret to encode (#1580)
Browse files Browse the repository at this point in the history
**Description of the change**
Kubeseal will throw an error when trying to seal no secrets. This will
happen when the file is empty or invalid, and the error suggests the
user to check the file encoding as this seems to be a common issue
especially in some versions of Windows #1410 #1560

**Benefits**
Give feedback to the user to help debugging a common issue

**Possible drawbacks**
Kubeseal will now throw an error where it previously just silently
succeeded with no output

**Applicable issues**
- fixes #1410
- fixes #1560

---------

Signed-off-by: Alejandro Moreno <[email protected]>
  • Loading branch information
alemorcuq authored Oct 4, 2024
1 parent de1f4dd commit c532fcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions integration/kubeseal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ var _ = Describe("kubeseal", func() {
})
})

var _ = Describe("kubeseal (with invalid input)", func() {
var input io.Reader
var output *bytes.Buffer
var args []string

BeforeEach(func() {
output = &bytes.Buffer{}
})

It("should throw an error", func() {
err := runKubeseal(args, input, output)
Expect(err).To(HaveOccurred())
})
})

var _ = Describe("kubeseal --fetch-cert", func() {
var c corev1.CoreV1Interface
var input io.Reader
Expand Down
5 changes: 5 additions & 0 deletions pkg/kubeseal/kubeseal.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func readSecrets(r io.Reader) ([]*v1.Secret, error) {
return nil, err
}
}

return secrets, nil
}

Expand Down Expand Up @@ -226,6 +227,10 @@ func Seal(clientConfig ClientConfig, outputFormat string, in io.Reader, out io.W
return err
}

if len(secrets) == 0 {
return fmt.Errorf("no secrets found. Ensure the input is valid and UTF-8 encoded")
}

for _, secret := range secrets {
if len(secret.Data) == 0 && len(secret.StringData) == 0 && !allowEmptyData {
return fmt.Errorf("secret.data is empty in input Secret, assuming this is an error and aborting. To work with empty data, --allow-empty-data can be used")
Expand Down

0 comments on commit c532fcf

Please sign in to comment.