rpmmd/repository: repoconfig pointers

Convert some of the fields in the `RepoConfig` struct
to pointers. Since `RepoConfig` will be used to convert
custom repositories to an array of `osbuild.YumRepository`,
we need to ensure that fields that are not set explicitly
are not saved to the `/etc/yum.repos.d` repository files.
This commit is contained in:
Gianluca Zuccarelli 2023-04-18 19:24:27 +01:00 committed by Tomáš Hozza
parent 75e2138878
commit d44703cdc8
14 changed files with 78 additions and 51 deletions

View file

@ -17,6 +17,7 @@ import (
"strings"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/container"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/distroregistry"
@ -203,8 +204,8 @@ func convertRepo(r repository) rpmmd.RepoConfig {
Metalink: r.Metalink,
MirrorList: r.MirrorList,
GPGKeys: keys,
CheckGPG: r.CheckGPG,
CheckRepoGPG: false,
CheckGPG: &r.CheckGPG,
CheckRepoGPG: common.ToPtr(false),
IgnoreSSL: false,
MetadataExpire: r.MetadataExpire,
RHSM: r.RHSM,

View file

@ -8,6 +8,7 @@ import (
"os"
"path"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/container"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/distroregistry"
@ -149,14 +150,16 @@ func main() {
keys = []string{repo.GPGKey}
}
repos[i] = rpmmd.RepoConfig{
Name: repoName,
BaseURLs: urls,
Metalink: repo.Metalink,
MirrorList: repo.MirrorList,
GPGKeys: keys,
CheckGPG: repo.CheckGPG,
PackageSets: repo.PackageSets,
RHSM: repo.RHSM,
Name: repoName,
BaseURLs: urls,
Metalink: repo.Metalink,
MirrorList: repo.MirrorList,
GPGKeys: keys,
CheckGPG: &repo.CheckGPG,
CheckRepoGPG: common.ToPtr(false),
IgnoreSSL: false,
PackageSets: repo.PackageSets,
RHSM: repo.RHSM,
}
}

View file

@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/stretchr/testify/assert"
)

View file

@ -1363,7 +1363,7 @@ func genRepoConfig(repo Repository) (*rpmmd.RepoConfig, error) {
}
if repo.CheckGpg != nil {
repoConfig.CheckGPG = *repo.CheckGpg
repoConfig.CheckGPG = repo.CheckGpg
}
if repo.Gpgkey != nil && *repo.Gpgkey != "" {
repoConfig.GPGKeys = []string{*repo.Gpgkey}
@ -1372,10 +1372,10 @@ func genRepoConfig(repo Repository) (*rpmmd.RepoConfig, error) {
repoConfig.IgnoreSSL = *repo.IgnoreSsl
}
if repo.CheckRepoGpg != nil {
repoConfig.CheckRepoGPG = *repo.CheckRepoGpg
repoConfig.CheckRepoGPG = repo.CheckRepoGpg
}
if repoConfig.CheckGPG && len(repoConfig.GPGKeys) == 0 {
if repoConfig.CheckGPG != nil && *repoConfig.CheckGPG && len(repoConfig.GPGKeys) == 0 {
return nil, HTTPError(ErrorNoGPGKey)
}

View file

@ -108,7 +108,7 @@ func TestRepoConfigConversion(t *testing.T) {
Metalink: "",
MirrorList: "",
GPGKeys: []string{"some-kind-of-key"},
CheckGPG: true,
CheckGPG: common.ToPtr(true),
IgnoreSSL: false,
MetadataExpire: "",
RHSM: false,
@ -131,7 +131,7 @@ func TestRepoConfigConversion(t *testing.T) {
BaseURLs: []string{"http://base.url"},
Metalink: "", // since BaseURL is specified, MetaLink is not copied
MirrorList: "", // since BaseURL is specified, MirrorList is not copied
CheckGPG: false,
CheckGPG: nil,
IgnoreSSL: true,
MetadataExpire: "",
RHSM: false,
@ -153,7 +153,7 @@ func TestRepoConfigConversion(t *testing.T) {
Name: "",
Metalink: "", // since MirrorList is specified, MetaLink is not copied
MirrorList: "http://example.org/mirrorlist",
CheckGPG: false,
CheckGPG: nil,
IgnoreSSL: true,
MetadataExpire: "",
RHSM: false,
@ -175,7 +175,7 @@ func TestRepoConfigConversion(t *testing.T) {
Name: "",
Metalink: "http://example.org/metalink",
MirrorList: "",
CheckGPG: false,
CheckGPG: nil,
IgnoreSSL: true,
MetadataExpire: "",
RHSM: true,

View file

@ -7,6 +7,7 @@ import (
"testing"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/container"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/distro/distro_test_common"
@ -203,7 +204,7 @@ func TestImageTypePipelineNames(t *testing.T) {
BaseURLs: []string{"http://payload.example.com"},
PackageSets: imageType.PayloadPackageSets(),
GPGKeys: []string{"payload-gpg-key"},
CheckGPG: true,
CheckGPG: common.ToPtr(true),
},
}
containers := make([]container.Spec, 0)

View file

@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/container"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/distroregistry"
@ -76,7 +77,7 @@ func TestDistro_Manifest(t *testing.T, pipelinePath string, prefix string, regis
Metalink: repo.Metalink,
MirrorList: repo.MirrorList,
GPGKeys: keys,
CheckGPG: repo.CheckGPG,
CheckGPG: common.ToPtr(repo.CheckGPG),
PackageSets: repo.PackageSets,
}
}

View file

@ -266,11 +266,18 @@ func (s *Solver) reposFromRPMMD(rpmRepos []rpmmd.RepoConfig) ([]repoConfig, erro
Metalink: rr.Metalink,
MirrorList: rr.MirrorList,
GPGKeys: rr.GPGKeys,
CheckGPG: rr.CheckGPG,
CheckRepoGPG: rr.CheckRepoGPG,
IgnoreSSL: rr.IgnoreSSL,
MetadataExpire: rr.MetadataExpire,
}
if rr.CheckGPG != nil {
dr.CheckGPG = *rr.CheckGPG
}
if rr.CheckRepoGPG != nil {
dr.CheckRepoGPG = *rr.CheckRepoGPG
}
if rr.RHSM {
if s.subscriptions == nil {
return nil, fmt.Errorf("This system does not have any valid subscriptions. Subscribe it before specifying rhsm: true in sources.")
@ -457,7 +464,9 @@ func (pkgs packageSpecs) toRPMMD(repos map[string]rpmmd.RepoConfig) []rpmmd.Pack
rpmDependencies[i].Arch = dep.Arch
rpmDependencies[i].RemoteLocation = dep.RemoteLocation
rpmDependencies[i].Checksum = dep.Checksum
rpmDependencies[i].CheckGPG = repo.CheckGPG
if repo.CheckGPG != nil {
rpmDependencies[i].CheckGPG = *repo.CheckGPG
}
rpmDependencies[i].IgnoreSSL = repo.IgnoreSSL
if repo.RHSM {
rpmDependencies[i].Secrets = "org.osbuild.rhsm"

View file

@ -6,6 +6,7 @@ import (
"net/http/httptest"
"os"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
)
@ -19,7 +20,7 @@ func NewTestServer() *testRepoServer {
testrepo := rpmmd.RepoConfig{
Name: "cs9-baseos",
BaseURLs: []string{server.URL},
CheckGPG: false,
CheckGPG: common.ToPtr(false),
IgnoreSSL: true,
RHSM: false,
}

View file

@ -33,8 +33,8 @@ type RepoConfig struct {
Metalink string `json:"metalink,omitempty"`
MirrorList string `json:"mirrorlist,omitempty"`
GPGKeys []string `json:"gpgkeys,omitempty"`
CheckGPG bool `json:"check_gpg,omitempty"`
CheckRepoGPG bool `json:"check_repo_gpg,omitempty"`
CheckGPG *bool `json:"check_gpg,omitempty"`
CheckRepoGPG *bool `json:"check_repo_gpg,omitempty"`
IgnoreSSL bool `json:"ignore_ssl,omitempty"`
MetadataExpire string `json:"metadata_expire,omitempty"`
RHSM bool `json:"rhsm,omitempty"`
@ -49,6 +49,9 @@ func (r *RepoConfig) Hash() string {
bts := func(b bool) string {
return fmt.Sprintf("%T", b)
}
bpts := func(b *bool) string {
return fmt.Sprintf("%T", b)
}
ats := func(s []string) string {
return strings.Join(s, "")
}
@ -56,8 +59,8 @@ func (r *RepoConfig) Hash() string {
r.Metalink+
r.MirrorList+
ats(r.GPGKeys)+
bts(r.CheckGPG)+
bts(r.CheckRepoGPG)+
bpts(r.CheckGPG)+
bpts(r.CheckRepoGPG)+
bts(r.IgnoreSSL)+
r.MetadataExpire+
bts(r.RHSM))))
@ -237,7 +240,7 @@ func loadRepositoriesFromFile(filename string) (map[string][]RepoConfig, error)
Metalink: repo.Metalink,
MirrorList: repo.MirrorList,
GPGKeys: keys,
CheckGPG: repo.CheckGPG,
CheckGPG: &repo.CheckGPG,
RHSM: repo.RHSM,
MetadataExpire: repo.MetadataExpire,
ImageTypeTags: repo.ImageTypeTags,

View file

@ -7,6 +7,7 @@ import (
"reflect"
"testing"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro/test_distro"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
"github.com/stretchr/testify/assert"
@ -218,8 +219,8 @@ func TestOldWorkerRepositoryCompatUnmarshal(t *testing.T) {
Metalink: "http://example.com/metalink",
MirrorList: "http://example.com/mirrorlist",
GPGKeys: []string{"key1", "key2"},
CheckGPG: true,
CheckRepoGPG: true,
CheckGPG: common.ToPtr(true),
CheckRepoGPG: common.ToPtr(true),
IgnoreSSL: true,
MetadataExpire: "test",
RHSM: true,
@ -266,8 +267,8 @@ func TestOldWorkerRepositoryCompatMarshal(t *testing.T) {
Metalink: "http://example.com/metalink",
MirrorList: "http://example.com/mirrorlist",
GPGKeys: []string{"key1", "key2"},
CheckGPG: true,
CheckRepoGPG: true,
CheckGPG: common.ToPtr(true),
CheckRepoGPG: common.ToPtr(true),
IgnoreSSL: true,
MetadataExpire: "test",
RHSM: true,

View file

@ -582,13 +582,19 @@ func (s *Store) GetAllDistroSources(distro string) map[string]SourceConfig {
func NewSourceConfig(repo rpmmd.RepoConfig, system bool) SourceConfig {
sc := SourceConfig{
Name: repo.Name,
CheckGPG: repo.CheckGPG,
CheckSSL: !repo.IgnoreSSL,
System: system,
RHSM: repo.RHSM,
CheckRepoGPG: repo.CheckRepoGPG,
GPGKeys: repo.GPGKeys,
Name: repo.Name,
System: system,
RHSM: repo.RHSM,
GPGKeys: repo.GPGKeys,
CheckSSL: !repo.IgnoreSSL,
}
if repo.CheckGPG != nil {
sc.CheckGPG = *repo.CheckGPG
}
if repo.CheckRepoGPG != nil {
sc.CheckRepoGPG = *repo.CheckRepoGPG
}
if len(repo.BaseURLs) != 0 {
@ -610,9 +616,9 @@ func (s *SourceConfig) RepoConfig(name string) rpmmd.RepoConfig {
repo.Name = name
repo.IgnoreSSL = !s.CheckSSL
repo.CheckGPG = s.CheckGPG
repo.CheckGPG = &s.CheckGPG
repo.RHSM = s.RHSM
repo.CheckRepoGPG = s.CheckRepoGPG
repo.CheckRepoGPG = &s.CheckRepoGPG
repo.GPGKeys = s.GPGKeys
var urls []string

View file

@ -422,7 +422,7 @@ func (suite *storeTest) TestNewSourceConfigWithBaseURL() {
myRepoConfig := rpmmd.RepoConfig{
Name: "testRepo",
BaseURLs: []string{"testURL"},
CheckGPG: true,
CheckGPG: common.ToPtr(true),
}
expectedSource := SourceConfig{Name: "testRepo", Type: "yum-baseurl", URL: "testURL", CheckGPG: true, CheckSSL: true, System: true}
actualSource := NewSourceConfig(myRepoConfig, true)
@ -433,7 +433,7 @@ func (suite *storeTest) TestNewSourceConfigWithMetaLink() {
myRepoConfig := rpmmd.RepoConfig{
Name: "testRepo",
Metalink: "testURL",
CheckGPG: true,
CheckGPG: common.ToPtr(true),
}
expectedSource := SourceConfig{Name: "testRepo", Type: "yum-metalink", URL: "testURL", CheckGPG: true, CheckSSL: true, System: true}
actualSource := NewSourceConfig(myRepoConfig, true)
@ -452,7 +452,7 @@ func (suite *storeTest) TestNewSourceConfigWithMirrorList() {
// Test converting a SourceConfig with GPGkeys to a RepoConfig
func (suite *storeTest) TestRepoConfigGPGKeys() {
expectedRepo := rpmmd.RepoConfig{Name: "testSourceConfig", BaseURLs: []string{"testURL"}, Metalink: "", MirrorList: "", IgnoreSSL: true, MetadataExpire: "", CheckRepoGPG: true, GPGKeys: []string{"http://path.to.gpgkeys/key.pub", "-----BEGIN PGP PUBLIC KEY BLOCK-----\nFULL GPG KEY HERE\n-----END PGP PUBLIC KEY BLOCK-----"}}
expectedRepo := rpmmd.RepoConfig{Name: "testSourceConfig", BaseURLs: []string{"testURL"}, Metalink: "", MirrorList: "", IgnoreSSL: true, MetadataExpire: "", CheckGPG: common.ToPtr(false), CheckRepoGPG: common.ToPtr(true), GPGKeys: []string{"http://path.to.gpgkeys/key.pub", "-----BEGIN PGP PUBLIC KEY BLOCK-----\nFULL GPG KEY HERE\n-----END PGP PUBLIC KEY BLOCK-----"}}
mySourceConfig := suite.mySourceConfig
mySourceConfig.Type = "yum-baseurl"
mySourceConfig.URL = "testURL"
@ -463,7 +463,7 @@ func (suite *storeTest) TestRepoConfigGPGKeys() {
}
func (suite *storeTest) TestRepoConfigBaseURL() {
expectedRepo := rpmmd.RepoConfig{Name: "testSourceConfig", BaseURLs: []string{"testURL"}, Metalink: "", MirrorList: "", IgnoreSSL: true, MetadataExpire: ""}
expectedRepo := rpmmd.RepoConfig{Name: "testSourceConfig", BaseURLs: []string{"testURL"}, Metalink: "", MirrorList: "", IgnoreSSL: true, CheckGPG: common.ToPtr(false), CheckRepoGPG: common.ToPtr(false), MetadataExpire: ""}
suite.mySourceConfig.Type = "yum-baseurl"
suite.mySourceConfig.URL = "testURL"
actualRepo := suite.mySourceConfig.RepoConfig("testSourceConfig")
@ -471,7 +471,7 @@ func (suite *storeTest) TestRepoConfigBaseURL() {
}
func (suite *storeTest) TestRepoConfigMetalink() {
expectedRepo := rpmmd.RepoConfig{Name: "testSourceConfig", Metalink: "testURL", MirrorList: "", IgnoreSSL: true, MetadataExpire: ""}
expectedRepo := rpmmd.RepoConfig{Name: "testSourceConfig", Metalink: "testURL", MirrorList: "", IgnoreSSL: true, CheckGPG: common.ToPtr(false), CheckRepoGPG: common.ToPtr(false), MetadataExpire: ""}
suite.mySourceConfig.Type = "yum-metalink"
suite.mySourceConfig.URL = "testURL"
actualRepo := suite.mySourceConfig.RepoConfig("testSourceConfig")
@ -479,7 +479,7 @@ func (suite *storeTest) TestRepoConfigMetalink() {
}
func (suite *storeTest) TestRepoConfigMirrorlist() {
expectedRepo := rpmmd.RepoConfig{Name: "testSourceConfig", Metalink: "", MirrorList: "testURL", IgnoreSSL: true, MetadataExpire: ""}
expectedRepo := rpmmd.RepoConfig{Name: "testSourceConfig", Metalink: "", MirrorList: "testURL", IgnoreSSL: true, CheckGPG: common.ToPtr(false), CheckRepoGPG: common.ToPtr(false), MetadataExpire: ""}
suite.mySourceConfig.Type = "yum-mirrorlist"
suite.mySourceConfig.URL = "testURL"
actualRepo := suite.mySourceConfig.RepoConfig("testSourceConfig")

View file

@ -63,12 +63,12 @@ func createWeldrAPI(tempdir string, fixtureGenerator rpmmd_mock.FixtureGenerator
rr := reporegistry.NewFromDistrosRepoConfigs(rpmmd.DistrosRepoConfigs{
test_distro.TestDistroName: {
test_distro.TestArchName: {
{Name: "test-id", BaseURLs: []string{"http://example.com/test/os/x86_64"}, CheckGPG: true},
{Name: "test-id", BaseURLs: []string{"http://example.com/test/os/x86_64"}, CheckGPG: common.ToPtr(true)},
},
},
test_distro.TestDistro2Name: {
test_distro.TestArchName: {
{Name: "test-id-2", BaseURLs: []string{"http://example.com/test-2/os/x86_64"}, CheckGPG: true},
{Name: "test-id-2", BaseURLs: []string{"http://example.com/test-2/os/x86_64"}, CheckGPG: common.ToPtr(true)},
},
},
})
@ -112,12 +112,12 @@ func createWeldrAPI2(tempdir string, fixtureGenerator rpmmd_mock.FixtureGenerato
rr := reporegistry.NewFromDistrosRepoConfigs(rpmmd.DistrosRepoConfigs{
test_distro.TestDistroName: {
test_distro.TestArch2Name: {
{Name: "test-id", BaseURLs: []string{"http://example.com/test/os/x86_64"}, CheckGPG: true},
{Name: "test-id", BaseURLs: []string{"http://example.com/test/os/x86_64"}, CheckGPG: common.ToPtr(true)},
},
},
test_distro.TestDistro2Name: {
test_distro.TestArch2Name: {
{Name: "test-id-2", BaseURLs: []string{"http://example.com/test-2/os/x86_64"}, CheckGPG: true},
{Name: "test-id-2", BaseURLs: []string{"http://example.com/test-2/os/x86_64"}, CheckGPG: common.ToPtr(true)},
},
},
})