Skip to content

kie-issues#1583: [sonataflow-operator] Add external built image integrity validation #3013

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

treblereel
Copy link
Contributor

@treblereel treblereel commented Mar 19, 2025

@treblereel treblereel requested a review from tiagobento as a code owner March 19, 2025 02:19
@treblereel treblereel requested review from ricardozanini and domhanak and removed request for tiagobento March 19, 2025 02:19
@treblereel treblereel self-assigned this Mar 19, 2025
@ricardozanini ricardozanini requested a review from wmedvede March 19, 2025 13:44
@@ -40,6 +40,7 @@ COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/
COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/*.jar /deployments/
COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/app/ /deployments/app/
COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/quarkus/ /deployments/quarkus/
COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/classes/workflow.sw.json /deployments/app/workflow.sw.json
Copy link
Member

Choose a reason for hiding this comment

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

So, should the builder image add the workflow file to the /deployments/app? Is there no other way to do it? This file can also be a YAML named anything (bananas.sw.yml).

So, if we imagine a custom builder or an image built by users, they must know where to put the file in their environment.

Docs and migration from a previous version will be severely impacted. Can you also add a configuration to the manager to skip this validation? It will be needed in case of a migration from a previous version.

Currently, users may build their workflow images as they wish. To migrate to this new version, they might need to turn the validation off and turn it on once everything is migrated.

@tiagobento tiagobento changed the title kie-issues#1583: [sonataflow-operator] Add external built image integ… kie-issues#1583: [sonataflow-operator] Add external built image integrity validation Mar 20, 2025
@treblereel
Copy link
Contributor Author

blocked by #3028

@treblereel treblereel force-pushed the incubator-kie-kogito-serverless-operator_405 branch from 6713238 to f4eaddc Compare March 28, 2025 17:27
@treblereel treblereel force-pushed the incubator-kie-kogito-serverless-operator_405 branch from c51a1c9 to c9f1816 Compare March 30, 2025 02:24
@@ -0,0 +1,128 @@
// Copyright 2024 Apache Software Foundation (ASF)
Copy link
Contributor

Choose a reason for hiding this comment

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

wrong licence format, copyright not needed

@@ -118,6 +122,11 @@ func (r *SonataFlowReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, err
}

if err := r.Validate(ctx, workflow, req); err != nil {
klog.V(log.E).ErrorS(err, "Failed to validate SonataFlow")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the error message should include the name / namespace of the failing workflow. That information will be very helpful when we need to see the logs in case of failues.

Copy link
Contributor

Choose a reason for hiding this comment

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

This invocation to the validator should go after the code below.
That if indicates that the WF being deleted right now, so, we don't need to validate it.

if workflow.DeletionTimestamp != nil {
	return r.applyFinalizers(ctx, workflow)
}


var validators = []Validator{ImageValidator{}}

type Validator interface {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice approach!

Validate(ctx context.Context, client client.Client, sonataflow *operatorapi.SonataFlow, req ctrl.Request) error
}

// validate the SonataFlow object, right now it's only check if workflow is in deployment has image declared as that image
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick, validate here, goes with uppercase Validate instead.


func (v ImageValidator) Validate(ctx context.Context, client client.Client, sonataflow *operatorapi.SonataFlow, req ctrl.Request) error {
if sonataflow.HasContainerSpecImage() {
err := client.Get(ctx, req.NamespacedName, sonataflow)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we are validating a `workflow' that was just read the instant before calling the Validators sequence, I don't believe we need to re-read it again.

But if you do, I don't believe we should read it and storing the value in the parameter sonataflow *operatorapi.SonataFlow paramter, I think this could make the validator has side effects on that variable value. When multiple validators are executed I think this code wont be clear.

Another nitpick, when we do reading of the resource as part of the reconciliation, we normally guard the code with something like this:

	err := r.Client.Get(ctx, req.NamespacedName, workflow)
	if err != nil {
		if errors.IsNotFound(err) {
			not good, return the error or whatever, but we know exactly that the given resource don't exist at this time.
		}
		not good, return the error or whatever, something went wrong, we don't know what.
	}

func (v ImageValidator) Validate(ctx context.Context, client client.Client, sonataflow *operatorapi.SonataFlow, req ctrl.Request) error {
if sonataflow.HasContainerSpecImage() {
err := client.Get(ctx, req.NamespacedName, sonataflow)
equals, err := validateImage(sonataflow, sonataflow.Spec.PodTemplate.Container.Image)
Copy link
Contributor

Choose a reason for hiding this comment

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

if we don't do the consideration above, in case of error the sonataflow value is uncertain.

return err
}
if !equals {
return fmt.Errorf("Workflow, defined in the image %s doesn't match deployment workflow", sonataflow.Spec.PodTemplate.Container.Image)
Copy link
Contributor

Choose a reason for hiding this comment

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

workflow name/namepace would be nice to include in the message.

func jsonFromDockerImage(reader io.Reader) (operatorapi.Flow, error) {
data, err := io.ReadAll(reader)
workflow := &operatorapi.Flow{}
if err = yaml.Unmarshal(data, workflow); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Excuse me if I'm missing something, but do this Unmarshalling work both when the workflow inside the image is in json and yaml format? If so, maybe this function can be called workflowFromDockerImage.

@@ -0,0 +1,41 @@
// Copyright 2024 Apache Software Foundation (ASF)
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, copyright we don't need

@wmedvede
Copy link
Contributor

wmedvede commented Apr 1, 2025

Hi @treblereel I had tried the feature in OpenShift 4.15.17 with the following scenario

  1. I have a workflow with a pre-built image
  2. In SonataFlow I modify the flow intentionally to force validation to fail
  3. When I deploy the workflow I can see this message:

E0401 16:52:11.167885 1 sonataflow_controller.go:126] "Failed to validate SonataFlow" err="Get \"https://quay.io/v2/\": tls: failed to verify certificate: x509: certificate signed by unknown authority"

Screenshot from 2025-04-01 18-55-10

``

It looks like it was not possible for the validator to read the image,

@wmedvede
Copy link
Contributor

wmedvede commented Apr 2, 2025

@treblereel I'd recommend rebasing with main, since there has been some recent updates in sontaflow_controller.go

@ricardozanini
Copy link
Member

@treblereel the docker api needs to authenticate with OpenShift internal registry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add external built image integrity validation
3 participants