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
38 changes: 37 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SYNOPSIS
[--push] [--username] [--password] [--token]
[--platform] [-p|--path] [-c|--confirm] [-v|--verbose]
[--build-timestamp] [--registry-insecure] [--registry-authfile]
[--build-cafile]

DESCRIPTION

Expand Down Expand Up @@ -71,7 +72,8 @@ EXAMPLES
SuggestFor: []string{"biuld", "buidl", "built"},
PreRunE: bindEnv("image", "path", "builder", "registry", "confirm",
"push", "builder-image", "base-image", "platform", "verbose",
"build-timestamp", "registry-insecure", "registry-authfile", "username", "password", "token"),
"build-timestamp", "registry-insecure", "registry-authfile", "username", "password", "token",
"build-cafile"),
RunE: func(cmd *cobra.Command, args []string) error {
return runBuild(cmd, args, newClient)
},
Expand Down Expand Up @@ -114,6 +116,7 @@ EXAMPLES
"Specify a custom builder image for use by the builder other than its default. ($FUNC_BUILDER_IMAGE)")
cmd.Flags().StringP("base-image", "", f.Build.BaseImage,
"Override the base image for your function (host builder only)")
cmd.Flags().String("build-cafile", f.Local.BuildCAFile, buildCAFileFlagUsage)
cmd.Flags().StringP("image", "i", f.Image,
"Full image name in the form [registry]/[namespace]/[name]:[tag] (optional). This option takes precedence over --registry ($FUNC_IMAGE)")

Expand Down Expand Up @@ -230,6 +233,12 @@ type buildConfig struct {
// TODO: gauron99 -- make option to add a path to dockerfile ?
BaseImage string

// BuildCAFile is a PEM file of CA certificates to trust during local pack
// builds. Relative paths are resolved against the function root. It is
// remembered in the function's local settings (.func/local.yaml), not in
// func.yaml, as it describes this machine rather than the function.
BuildCAFile string

// Path of the function implementation on local disk. Defaults to current
// working directory of the process.
Path string
Expand Down Expand Up @@ -271,6 +280,7 @@ func newBuildConfig() buildConfig {
},
BuilderImage: viper.GetString("builder-image"),
BaseImage: viper.GetString("base-image"),
BuildCAFile: viper.GetString("build-cafile"),
Image: viper.GetString("image"),
Path: viper.GetString("path"),
Platform: viper.GetString("platform"),
Expand All @@ -294,6 +304,7 @@ func (c buildConfig) Configure(f fn.Function) fn.Function {
}
f.Image = c.Image
f.Build.BaseImage = c.BaseImage
f.Local.BuildCAFile = c.BuildCAFile
// Path, Platform and Push are not part of a function's state.
return f
}
Expand Down Expand Up @@ -423,10 +434,35 @@ func (c buildConfig) Validate(cmd *cobra.Command) (err error) {
// BaseImage is only supported with the host builder
if c.BaseImage != "" && c.Builder != "host" {
err = errors.New("only host builds support specifying the base image")
return
}

// A build CA file is only supported with the pack builder. A value
// remembered in .func/local.yaml is ignored by other builders, so only
// reject one given on this command.
if c.Builder != builders.Pack && flagGiven(cmd, "build-cafile") {
err = ErrBuildCAFileRequiresPack
}
return
}

// buildCAFileFlagUsage is the help text of the --build-cafile flag, shared by
// the build, deploy and run commands.
const buildCAFileFlagUsage = "Path to a PEM file with one or more CA certificates to trust during the build, " +
"for example the CA of a TLS-intercepting proxy. Used at build time only and not added to the " +
"function image. Pack builder only. A relative path is resolved against the function directory. " +
"Remembered for this copy of the function in .func/local.yaml, not in func.yaml. " +
"git does not read it on its own, so dependencies fetched with git (e.g. Go modules from a VCS) " +
"need the GIT_SSL_CAINFO build env as well, see docs/building-functions/custom-ca.md. ($FUNC_BUILD_CAFILE)"

// ErrBuildCAFileRequiresPack is returned when --build-cafile is used with a
// builder other than pack.
var ErrBuildCAFileRequiresPack = errors.New("--build-cafile is only supported by the pack builder")

// ErrBuildCAFileNotRemote is returned when --build-cafile is given for a
// remote (on-cluster) build, which does not support it yet.
var ErrBuildCAFileNotRemote = errors.New("--build-cafile is not supported for remote (on-cluster) builds yet")

// clientOptions returns options suitable for instantiating a client based on
// the current state of the build config object.
// This will be unnecessary and refactored away when the host-based OCI
Expand Down
93 changes: 93 additions & 0 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cmd

import (
"errors"
"os"
"path/filepath"
"strings"
"testing"

fn "knative.dev/func/pkg/functions"
Expand Down Expand Up @@ -95,6 +98,12 @@ func TestBuild_BaseImage(t *testing.T) {
testBaseImage(NewBuildCmd, t)
}

// TestBuild_BuildCAFile ensures that --build-cafile is accepted with the pack
// builder only and is remembered in the function's local settings.
func TestBuild_BuildCAFile(t *testing.T) {
testBuildCAFile(NewBuildCmd, t)
}

// TestBuild_Push ensures that the build command properly pushes and respects
// the --push flag.
// - Push triggered after a successful build
Expand Down Expand Up @@ -152,3 +161,87 @@ func TestBuild_Push(t *testing.T) {
t.Fatal("push should not be invoked on a failed build")
}
}

// testBuildCAFile ensures the build CA file is read from the function's local
// settings (.func/local.yaml), the FUNC_BUILD_CAFILE env and the
// --build-cafile flag, reaches the builder, and is remembered in
// .func/local.yaml but never written to func.yaml. Given on the command with a
// builder other than pack it is an error; a remembered value is ignored there.
func testBuildCAFile(cmdFn commandConstructor, t *testing.T) {
tests := []struct {
name string
builder string
remembered string // value already in .func/local.yaml
env string // FUNC_BUILD_CAFILE
args []string
want string
wantErr error
}{
{name: "flag", builder: "pack", args: []string{"--build-cafile=certs/ca.pem"}, want: "certs/ca.pem"},
{name: "env", builder: "pack", env: "/etc/corp/ca.pem", want: "/etc/corp/ca.pem"},
{name: "remembered", builder: "pack", remembered: "ca.pem", want: "ca.pem"},
{name: "flag overrides remembered", builder: "pack", remembered: "ca.pem", args: []string{"--build-cafile=other.pem"}, want: "other.pem"},
{name: "flag clears remembered", builder: "pack", remembered: "ca.pem", args: []string{"--build-cafile="}, want: ""},
{name: "s2i rejects flag", builder: "s2i", args: []string{"--build-cafile=ca.pem"}, wantErr: ErrBuildCAFileRequiresPack},
{name: "s2i rejects env", builder: "s2i", env: "ca.pem", wantErr: ErrBuildCAFileRequiresPack},
{name: "host rejects flag", builder: "host", args: []string{"--build-cafile=ca.pem"}, wantErr: ErrBuildCAFileRequiresPack},
// A value remembered for this machine must not break other builders.
{name: "s2i ignores remembered", builder: "s2i", remembered: "ca.pem", want: "ca.pem"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := FromTempDirectory(t)
f, err := fn.New().Init(fn.Function{Runtime: "go", Root: root, Registry: TestRegistry,
Local: fn.Local{BuildCAFile: tt.remembered}})
if err != nil {
t.Fatal(err)
}
if err = f.Write(); err != nil {
t.Fatal(err)
}
if tt.env != "" {
t.Setenv("FUNC_BUILD_CAFILE", tt.env)
}

builder := mock.NewBuilder()
builder.BuildFn = func(f fn.Function) error {
if f.Local.BuildCAFile != tt.want {
t.Errorf("expected builder to receive CA file %q, got %q", tt.want, f.Local.BuildCAFile)
}
return nil
}
cmd := cmdFn(NewTestClient(
fn.WithBuilder(builder),
fn.WithDeployer(mock.NewDeployer()),
fn.WithRegistry(TestRegistry),
))
cmd.SetArgs(append([]string{"--builder=" + tt.builder}, tt.args...))
err = cmd.Execute()
if tt.wantErr != nil {
if !errors.Is(err, tt.wantErr) {
t.Fatalf("expected error %v, got %v", tt.wantErr, err)
}
return
}
if err != nil {
t.Fatal(err)
}
if !builder.BuildInvoked {
t.Fatal("builder was not invoked")
}
if f, err = fn.NewFunction(root); err != nil {
t.Fatal(err)
}
if f.Local.BuildCAFile != tt.want {
t.Fatalf("expected .func/local.yaml buildCAFile %q, got %q", tt.want, f.Local.BuildCAFile)
}
funcYAML, err := os.ReadFile(filepath.Join(root, fn.FunctionFile))
if err != nil {
t.Fatal(err)
}
if strings.Contains(strings.ToLower(string(funcYAML)), "cafile") {
t.Fatalf("the CA file must not be written to func.yaml:\n%s", funcYAML)
}
})
}
}
13 changes: 12 additions & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SYNOPSIS
[--domain] [--platform] [--build-timestamp] [--pvc-size]
[--service-account] [-c|--confirm] [-v|--verbose]
[--registry-insecure] [--registry-authfile] [--remote-storage-class]
[--build-cafile]

DESCRIPTION

Expand Down Expand Up @@ -157,7 +158,8 @@ EXAMPLES
"source", "image", "image-pull-secret", "management-disabled",
"namespace", "path", "platform", "push", "pvc-size", "service-account",
"deployer", "expose", "registry", "registry-insecure", "registry-authfile",
"remote", "username", "password", "token", "verbose", "remote-storage-class"),
"remote", "username", "password", "token", "verbose", "remote-storage-class",
"build-cafile"),
RunE: func(cmd *cobra.Command, args []string) error {
return runDeploy(cmd, newClient)
},
Expand Down Expand Up @@ -197,6 +199,7 @@ EXAMPLES
"Specify a custom builder image for use by the builder other than its default. ($FUNC_BUILDER_IMAGE)")
cmd.Flags().StringP("base-image", "", f.Build.BaseImage,
"Override the base image for your function (host builder only)")
cmd.Flags().String("build-cafile", f.Local.BuildCAFile, buildCAFileFlagUsage)
cmd.Flags().StringP("image", "i", f.Image,
"Full image name in the form [registry]/[namespace]/[name]:[tag]@[digest]. This option takes precedence over --registry. Specifying digest is optional, but if it is given, 'build' and 'push' phases are disabled. ($FUNC_IMAGE)")

Expand Down Expand Up @@ -855,6 +858,14 @@ func (c deployConfig) Validate(cmd *cobra.Command) (err error) {
return errors.New("source settings (--source, --revision and --source-dir) are only applicable when triggering remote deployments (--remote)")
}

// A build CA file is only applied to local pack builds; the on-cluster
// pipelines do not support it yet. A value remembered in
// .func/local.yaml is ignored by remote builds, so only reject one given
// on this command.
if c.Remote && flagGiven(cmd, "build-cafile") {
return ErrBuildCAFileNotRemote
}

// Source can contain at maximum one '#'
urlParts := strings.Split(c.Source, "#")
if len(urlParts) > 2 {
Expand Down
69 changes: 69 additions & 0 deletions cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,75 @@ func Test_isDigested(t *testing.T) {
}
}

// TestDeploy_BuildCAFile ensures that --build-cafile is accepted with the pack
// builder only and is remembered in the function's local settings.
func TestDeploy_BuildCAFile(t *testing.T) {
testBuildCAFile(NewDeployCmd, t)
}

// TestDeploy_BuildCAFileRemote ensures that a build CA file given on a remote
// (on-cluster) deploy is rejected, as the pipelines do not support it yet,
// while one remembered in .func/local.yaml for local builds is ignored by the
// remote deploy and kept for the next local build.
func TestDeploy_BuildCAFileRemote(t *testing.T) {
tests := []struct {
name string
remembered string // value already in .func/local.yaml
env string // FUNC_BUILD_CAFILE
args []string
wantErr bool
}{
{name: "flag rejected", args: []string{"--build-cafile=ca.pem"}, wantErr: true},
{name: "env rejected", env: "ca.pem", wantErr: true},
{name: "remembered ignored", remembered: "ca.pem"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := FromTempDirectory(t)
f, err := fn.New().Init(fn.Function{Runtime: "go", Root: root, Registry: TestRegistry,
Local: fn.Local{BuildCAFile: tt.remembered}})
if err != nil {
t.Fatal(err)
}
if err = f.Write(); err != nil {
t.Fatal(err)
}
if tt.env != "" {
t.Setenv("FUNC_BUILD_CAFILE", tt.env)
}
pipelinesProvider := mock.NewPipelinesProvider()
cmd := NewDeployCmd(NewTestClient(
fn.WithPipelinesProvider(pipelinesProvider),
fn.WithRegistry(TestRegistry),
))
cmd.SetArgs(append([]string{"--remote", "--builder=pack"}, tt.args...))
err = cmd.Execute()

if tt.wantErr {
if !errors.Is(err, ErrBuildCAFileNotRemote) {
t.Fatalf("expected %v, got %v", ErrBuildCAFileNotRemote, err)
}
if pipelinesProvider.RunInvoked {
t.Fatal("pipeline must not run")
}
return
}
if err != nil {
t.Fatalf("a remembered CA file must not fail a remote deploy: %v", err)
}
if !pipelinesProvider.RunInvoked {
t.Fatal("expected the pipeline to run")
}
if f, err = fn.NewFunction(root); err != nil {
t.Fatal(err)
}
if f.Local.BuildCAFile != tt.remembered {
t.Fatalf("expected the remembered CA file %q to be kept, got %q", tt.remembered, f.Local.BuildCAFile)
}
})
}
}

func TestDeploy_BaseImage(t *testing.T) {
testBaseImage(NewDeployCmd, t)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ NAME
SYNOPSIS
{{rootCmdUse}} run [-r|--registry] [-i|--image] [-e|--env] [--build]
[-b|--builder] [--builder-image] [-c|--confirm]
[--address] [--json] [-v|--verbose]
[--address] [--json] [-v|--verbose] [--build-cafile]

DESCRIPTION
Run the function locally.
Expand Down Expand Up @@ -74,7 +74,7 @@ EXAMPLES
SuggestFor: []string{"rnu"},
PreRunE: bindEnv("build", "builder", "builder-image", "base-image",
"confirm", "env", "image", "path", "registry",
"start-timeout", "verbose", "address", "json"),
"start-timeout", "verbose", "address", "json", "build-cafile"),
RunE: func(cmd *cobra.Command, _ []string) error {
return runRun(cmd, newClient)
},
Expand Down Expand Up @@ -108,6 +108,7 @@ EXAMPLES
"Specify a custom builder image for use by the builder other than its default. ($FUNC_BUILDER_IMAGE)")
cmd.Flags().StringP("base-image", "", f.Build.BaseImage,
"Override the base image for your function (host builder only)")
cmd.Flags().String("build-cafile", f.Local.BuildCAFile, buildCAFileFlagUsage)
cmd.Flags().StringP("image", "i", f.Image,
"Full image name in the form [registry]/[namespace]/[name]:[tag]. This option takes precedence over --registry. Specifying tag is optional. ($FUNC_IMAGE)")
cmd.Flags().StringArrayP("env", "e", []string{},
Expand Down
Loading
Loading