Skip to content
Open
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 .github/workflows/build-verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
make clean
make build-binaries

- name: Run tests
run: go test ./...

- name: Set environment for branch
run: |
set -x
Expand Down
62 changes: 62 additions & 0 deletions .github/workflows/policy-guards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: policy-guards

on:
push:
paths:
- '**/*.go'
- '.github/workflows/policy-guards.yml'
pull_request:
paths:
- '**/*.go'
- '.github/workflows/policy-guards.yml'

permissions:
contents: read

jobs:
policy-guards:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2

- name: Guard Go copyright headers
run: |
missing=""
while IFS= read -r file; do
if ! grep -q "Copyright The Microcks Authors" "$file"; then
missing+="${file}"$'\n'
fi
done < <(git ls-files '*.go')

if [[ -n "$missing" ]]; then
echo "::error::Go files missing Microcks copyright header:"
printf "%s" "$missing"
exit 1
fi

- name: Guard against silently ignored errors
run: |
# Avoid reintroducing review issues where malformed responses or local
# failures are discarded instead of being returned as classified errors.
if grep -rnE '(^|[^[:alnum:]_])_ =|, _ :=|fmt\.Println\(err\)' --include='*.go' cmd pkg; then
echo "::error::Potential silently ignored error found. Handle it explicitly or return errors.Wrap(kind, err)."
exit 1
fi
Comment on lines +42 to +45

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The silently-ignored-error grep pattern _ =|, _ :=|fmt\.Println\(err\) is a nice guard, but it's a blunt regex; it'll false-positive on any deliberate _ = x (e.g. explicitly discarding a value that's genuinely fine to ignore, if that ever comes up) with no allowlist mechanism. Not blocking, just flag it as something that may need a // nolint-style escape hatch down the line if a legitimate case shows up.


- name: Guard against stray process exits
run: |
# Library and command packages must return classified errors, never
# exit or panic. Only the explicit process entrypoints may exit.
# See documentation/error-handling.md.
matches="$(git grep -nE '(os\.Exit|log\.Fatal|panic\()' -- '*.go' \
':!**/*_test.go' \
':!cmd/exit.go' \
':!main.go' \
':!watcher/main.go' || true)"

if [[ -n "$matches" ]]; then
printf "%s\n" "$matches"
echo "::error::os.Exit/log.Fatal/panic found outside approved entrypoints — return errors.Wrap(kind, err) instead."
exit 1
fi
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ Microcks has adopted a Code of Conduct that we expect project participants to ad

We use Github to host code, to track issues and feature requests, as well as accept pull requests.

## Error handling

Code under `pkg/` and `cmd/` must return errors, never exit or panic on a runtime
error: wrap the failure with a Kind (`return errors.Wrap(errors.KindConnection, err)`)
and let it flow up. Only the `main` entrypoints and `cmd.Handle` exit the process.
See [documentation/error-handling.md](documentation/error-handling.md); CI enforces this.

## Issues

[Open an issue](https://github.com/microcks/microcks/issues/new) **only** if you want to report a bug or a feature. Don't open issues for questions or support, instead join our [Discord #support channel](https://microcks.io/discord-invite) or our [GitHub discussions](https://github.com/orgs/microcks/discussions) and ask there.
Expand Down
31 changes: 24 additions & 7 deletions cmd/context.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
/*
* Copyright The Microcks Authors.
*
* 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 cmd

import (
"fmt"
"log"
"os"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -81,8 +96,8 @@ func deleteContext(context, configPath string) error {
if !ok {
return errors.Wrapf(errors.KindNotFound, "context %q does not exist", context)
}
_ = localCfg.RemoveUser(context)
_ = localCfg.RemoveServer(serverName)
localCfg.RemoveUser(context)
localCfg.RemoveServer(serverName)

if localCfg.IsEmpty() {
if err := localCfg.DeleteLocalConfig(configPath); err != nil {
Expand Down Expand Up @@ -112,24 +127,26 @@ func printMicrocksContexts(configPath string) error {
return errors.Wrapf(errors.KindUsage, "no contexts defined in %s", configPath)
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
defer func() { _ = w.Flush() }()
columnNames := []string{"CURRENT", "NAME", "SERVER"}
if _, err = fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t")); err != nil {
return err
return errors.Wrap(errors.KindEnvironment, fmt.Errorf("writing contexts output: %w", err))
}

for _, contextRef := range localCfg.Contexts {
context, err := localCfg.ResolveContext(contextRef.Name)
if err != nil {
log.Printf("Context '%s' had error: %v", contextRef.Name, err)
return errors.Wrap(errors.KindUsage, fmt.Errorf("context %q is invalid: %w", contextRef.Name, err))
}
prefix := " "
if localCfg.CurrentContext == context.Name {
prefix = "*"
}
if _, err = fmt.Fprintf(w, "%s\t%s\t%s\n", prefix, context.Name, context.Server.Server); err != nil {
return err
return errors.Wrap(errors.KindEnvironment, fmt.Errorf("writing contexts output: %w", err))
}
}
if err := w.Flush(); err != nil {
return errors.Wrap(errors.KindEnvironment, fmt.Errorf("writing contexts output: %w", err))
}
return nil
}
16 changes: 16 additions & 0 deletions cmd/context_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The Microcks Authors.
*
* 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 cmd

import (
Expand Down
22 changes: 20 additions & 2 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The Microcks Authors.
*
* 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 cmd

import (
Expand Down Expand Up @@ -153,7 +169,7 @@ microcks login http://localhost:8080 --sso --sso-launch-browser=false
_, _, err = parser.ParseUnverified(authToken, &claims)

if err != nil {
fmt.Println(err)
return errors.Wrap(errors.KindAPI, fmt.Errorf("parsing authentication token: %w", err))
}

em := StringField(claims, "preferred_username")
Expand Down Expand Up @@ -316,7 +332,9 @@ func oauth2login(
fmt.Printf("Authentication successful\n")
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
if err := srv.Shutdown(ctx); err != nil {
return "", "", errors.Wrap(errors.KindEnvironment, fmt.Errorf("shutting down temporary HTTP server: %w", err))
}

return tokenString, refreshToken, nil
}
Expand Down
18 changes: 17 additions & 1 deletion cmd/logout.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The Microcks Authors.
*
* 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 cmd

import (
Expand Down Expand Up @@ -58,7 +74,7 @@ func logoutContext(target, configPath string) error {

err = config.ValidateLocalConfig(*localCfg)
if err != nil {
return fmt.Errorf("Error in loging out: %s", err)
return errors.Wrap(errors.KindUsage, fmt.Errorf("logging out leaves local config invalid: %w", err))
}

return config.WriteLocalConfig(*localCfg, configPath)
Expand Down
16 changes: 16 additions & 0 deletions cmd/logout_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The Microcks Authors.
*
* 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 cmd

import (
Expand Down
20 changes: 18 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The Microcks Authors.
*
* 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 cmd

import (
Expand Down Expand Up @@ -47,8 +63,8 @@ microcks start --name [name of you container/instance]`,
localConfig = &config.LocalConfig{}
}

instance, _ := localConfig.GetInstance(name)
if instance == nil {
instance, err := localConfig.GetInstance(name)
if err != nil {
instance = &config.Instance{}
}

Expand Down
16 changes: 16 additions & 0 deletions cmd/start_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The Microcks Authors.
*
* 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 cmd

import (
Expand Down
24 changes: 20 additions & 4 deletions cmd/stop.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The Microcks Authors.
*
* 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 cmd

import (
Expand Down Expand Up @@ -59,10 +75,10 @@ func NewStopCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
if !ok {
return errors.Wrapf(errors.KindNotFound, "context %q does not exist", ctx.Name)
}
_ = localConfig.RemoveServer(ctx.Server.Server)
_ = localConfig.RemoveUser(ctx.User.Name)
_ = localConfig.RemoveAuth(ctx.Server.Server)
_ = localConfig.RemoveInstance(instance.Name)
localConfig.RemoveServer(ctx.Server.Server)
localConfig.RemoveUser(ctx.User.Name)
localConfig.RemoveAuth(ctx.Server.Server)
localConfig.RemoveInstance(instance.Name)

localConfig.CurrentContext = ""
log.Printf("Instance %s removed successfully", instance.Name)
Expand Down
8 changes: 4 additions & 4 deletions cmd/testDryRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ func setupPodman() error {

func validateDryRunOptions(opts dryRunOptions) error {
if opts.artifact == "" {
return fmt.Errorf("--artifact is required with --dry-run")
return errors.Wrapf(errors.KindUsage, "--artifact is required with --dry-run")
}
if _, err := os.Stat(opts.artifact); err != nil {
return fmt.Errorf("cannot read --artifact file %q: %v", opts.artifact, err)
return errors.Wrap(errors.KindUsage, fmt.Errorf("cannot read --artifact file %q: %w", opts.artifact, err))
}
// The uber-native flavor runs without Keycloak, which is what makes the
// zero-config dry-run possible. Fail fast on other flavors.
if !strings.Contains(opts.image, "-native") {
return fmt.Errorf("--dry-run requires the uber-native image variant (got %q). "+
return errors.Wrapf(errors.KindUsage, "--dry-run requires the uber-native image variant (got %q). "+
"Use the default or pass --image with a *-native tag", opts.image)
}
return nil
Expand Down Expand Up @@ -144,7 +144,7 @@ func rewriteLocalEndpoint(testEndpoint string) (string, int, bool) {

func runDryRunTest(opts dryRunOptions) error {
if err := validateDryRunOptions(opts); err != nil {
return errors.Wrap(errors.KindUsage, err)
return err
}

// Select the container runtime (docker default, podman wired via DOCKER_HOST).
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The Microcks Authors.
*
* 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 main

import (
Expand Down
Loading