Skip to content

Commit

Permalink
ostree: drop RHSM for MTLS over ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed Oct 10, 2024
1 parent 3a705cb commit d75dffc
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ __pycache__
/test/data/manifests
/tools/appsre-ansible/inventory
dictionary.dic
/cmd/ostree-resolve/*.crt
/cmd/ostree-resolve/*.key

*~
25 changes: 25 additions & 0 deletions cmd/ostree-resolve/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"os"

"github.com/osbuild/images/pkg/ostree"
)

func main() {
fmt.Println("Resolving ostree source, configuration:")
fmt.Printf("CA: %s\n", os.Getenv("OSBUILD_COMPOSER_OSTREE_CA"))
fmt.Printf("Client cert: %s\n", os.Getenv("OSBUILD_COMPOSER_OSTREE_CLIENT_CERT"))
fmt.Printf("Client key: %s\n", os.Getenv("OSBUILD_COMPOSER_OSTREE_CLIENT_KEY"))

spec := ostree.SourceSpec{
URL: "https://builder.home.lan/ccb2194f-9876-4e76-9e64-a338a32df230/",
Ref: "fedora/40/x86_64/iot",
}
cs, err := ostree.Resolve(spec)
if err != nil {
panic(err)
}
fmt.Printf("Resolved checksum: %s", cs.Checksum)
}
10 changes: 4 additions & 6 deletions pkg/distro/fedora/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,8 @@ func makeOSTreeParentCommit(options *ostree.ImageOptions, defaultRef string) (*o

}
parentCommit = &ostree.SourceSpec{
URL: options.URL,
Ref: parentRef,
RHSM: options.RHSM,
URL: options.URL,
Ref: parentRef,
}
return parentCommit, commitRef
}
Expand All @@ -834,9 +833,8 @@ func makeOSTreePayloadCommit(options *ostree.ImageOptions, defaultRef string) (o
}

return ostree.SourceSpec{
URL: options.URL,
Ref: commitRef,
RHSM: options.RHSM,
URL: options.URL,
Ref: commitRef,
}, nil
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/distro/rhel/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,6 @@ func makeOSTreeParentCommit(options *ostree.ImageOptions, defaultRef string) (*o
parentCommit = &ostree.SourceSpec{
URL: options.URL,
Ref: parentRef,
RHSM: options.RHSM,
}
return parentCommit, commitRef
}
Expand All @@ -803,6 +802,5 @@ func makeOSTreePayloadCommit(options *ostree.ImageOptions, defaultRef string) (o
return ostree.SourceSpec{
URL: options.URL,
Ref: commitRef,
RHSM: options.RHSM,
}, nil
}
1 change: 0 additions & 1 deletion pkg/distro/test_distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ func (t *TestImageType) Manifest(b *blueprint.Blueprint, options distro.ImageOpt
}
// copy any other options that might be specified
ostreeSource.URL = options.OSTree.URL
ostreeSource.RHSM = options.OSTree.RHSM
}
ostreeSources = []ostree.SourceSpec{ostreeSource}
}
Expand Down
59 changes: 20 additions & 39 deletions pkg/ostree/ostree.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"regexp"
"strings"
"time"

"github.com/osbuild/images/pkg/rhsm"
)

var (
Expand All @@ -25,9 +23,8 @@ var (
// SourceSpec serves as input for ResolveParams, and contains all necessary
// variables to resolve a ref, which can then be turned into a CommitSpec.
type SourceSpec struct {
URL string
Ref string
RHSM bool
URL string
Ref string
}

// CommitSpec specifies an ostree commit using any combination of Ref (branch), URL (source), and Checksum (commit ID).
Expand Down Expand Up @@ -68,10 +65,6 @@ type ImageOptions struct {

// If specified, the URL will be used only for metadata.
ContentURL string `json:"contenturl"`

// Indicate if the 'org.osbuild.rhsm.consumer' secret should be added when pulling from the
// remote.
RHSM bool `json:"rhsm"`
}

// Validate the image options. This doesn't verify the existence of any remote
Expand Down Expand Up @@ -141,47 +134,39 @@ func verifyChecksum(commit string) bool {
// ResolveRef resolves the URL path specified by the location and ref
// (location+"refs/heads/"+ref) and returns the commit ID for the named ref. If
// there is an error, it will be of type ResolveRefError.
func ResolveRef(location, ref string, consumerCerts bool, subs *rhsm.Subscriptions, ca *string) (string, error) {
func ResolveRef(location, ref string) (string, error) {
u, err := url.Parse(location)
if err != nil {
return "", NewResolveRefError("error parsing ostree repository location: %v", err)
}
u.Path = path.Join(u.Path, "refs/heads/", ref)

var client *http.Client
if consumerCerts {
if subs == nil {
subs, err = rhsm.LoadSystemSubscriptions()
if u.Scheme == "https" {
tlsConf := &tls.Config{}

// If CA is set, load the CA certificate and add it to the TLS configuration. Otherwise, use the system CA.
if caFilename := os.Getenv("OSBUILD_COMPOSER_OSTREE_CA"); caFilename != "" {
caCertPEM, err := os.ReadFile(caFilename)
if err != nil {
return "", NewResolveRefError("error adding rhsm certificates when resolving ref: %s", err)
return "", NewResolveRefError("error adding ca certificate when resolving ref: %s", err)
}
if subs.Consumer == nil {
return "", NewResolveRefError("error adding rhsm certificates when resolving ref")
tlsConf.RootCAs = x509.NewCertPool()
if ok := tlsConf.RootCAs.AppendCertsFromPEM(caCertPEM); !ok {
return "", NewResolveRefError("error adding ca certificate when resolving ref")
}
}

tlsConf := &tls.Config{
MinVersion: tls.VersionTLS12,
}
certFilename := os.Getenv("OSBUILD_COMPOSER_OSTREE_CLIENT_CERT")
keyFilename := os.Getenv("OSBUILD_COMPOSER_OSTREE_CLIENT_KEY")

if ca != nil {
caCertPEM, err := os.ReadFile(*ca)
if certFilename != "" && keyFilename != "" {
cert, err := tls.LoadX509KeyPair(certFilename, keyFilename)
if err != nil {
return "", NewResolveRefError("error adding rhsm certificates when resolving ref: %s", err)
return "", NewResolveRefError("error adding client certificate when resolving ref: %s", err)
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(caCertPEM)
if !ok {
return "", NewResolveRefError("error adding rhsm certificates when resolving ref")
}
tlsConf.RootCAs = roots
}

cert, err := tls.LoadX509KeyPair(subs.Consumer.ConsumerCert, subs.Consumer.ConsumerKey)
if err != nil {
return "", NewResolveRefError("error adding rhsm certificates when resolving ref: %s", err)
tlsConf.Certificates = []tls.Certificate{cert}
}
tlsConf.Certificates = []tls.Certificate{cert}

client = &http.Client{
Transport: &http.Transport{
Expand Down Expand Up @@ -234,10 +219,6 @@ func Resolve(source SourceSpec) (CommitSpec, error) {
URL: source.URL,
}

if source.RHSM {
commit.Secrets = "org.osbuild.rhsm.consumer"
}

if verifyChecksum(source.Ref) {
// the ref is a commit: return as is
commit.Checksum = source.Ref
Expand All @@ -252,7 +233,7 @@ func Resolve(source SourceSpec) (CommitSpec, error) {
// URL set: Resolve checksum
if source.URL != "" {
// If a URL is specified, we need to fetch the commit at the URL.
checksum, err := ResolveRef(source.URL, source.Ref, source.RHSM, nil, nil)
checksum, err := ResolveRef(source.URL, source.Ref)
if err != nil {
return CommitSpec{}, err // ResolveRefError
}
Expand Down

0 comments on commit d75dffc

Please sign in to comment.