Skip to content

Commit

Permalink
Fix apache#2834: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpoth authored and bouskaJ committed Apr 7, 2022
1 parent acf9aa0 commit b3f2968
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 75 deletions.
41 changes: 19 additions & 22 deletions pkg/builder/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func generateProjectSettings(ctx *builderContext) error {
return nil
}

//Visible for testing
func injectServersIntoMavenSettings(settings string, servers []v1.Server) string {
if servers == nil || len(servers) < 1 {
return settings
Expand Down Expand Up @@ -145,37 +144,35 @@ func encodeXMLNoHeader(content interface{}) ([]byte, error) {
return w.Bytes(), nil
}

// Return Index of </server> Tag in val. Creates Tag if necessary
// Return Index of </server> Tag in val. Creates Tag if necessary.
func getServerTagIndex(val string) (string, int) {
serversTag := "\n <servers></servers>\n"
val = strings.Replace(val, "<servers/>", serversTag, 1)
endServerTag := "</servers>"
i := strings.Index(val, endServerTag)
if i > 0 {
return val, i
} else {
// create necessary tags
tags := []string{"</proxies>", "<proxies/>", "</offline>", "<offline/>", "</usePluginRegistry>", "<usePluginRegistry/>", "</interactiveMode>", "<interactiveMode/>", "</localRepository>", "<localRepository/>"}
i = -1
for _, tag := range tags {
i = strings.Index(val, tag)
if i > 0 {
i += len(tag)
break
}
}
// create necessary tags
tags := []string{"</proxies>", "<proxies/>", "</offline>", "<offline/>", "</usePluginRegistry>", "<usePluginRegistry/>", "</interactiveMode>", "<interactiveMode/>", "</localRepository>", "<localRepository/>"}
i = -1
for _, tag := range tags {
i = strings.Index(val, tag)
if i > 0 {
i += len(tag)
break
}
if i < 0 {
regexp := regexp.MustCompile(`<settings.*>`)
loc := regexp.FindStringIndex(val)
if loc == nil {
return val, i
} else {
i = loc[1]
}
}
if i < 0 {
regexp := regexp.MustCompile(`<settings.*>`)
loc := regexp.FindStringIndex(val)
if loc == nil {
return val, i
}
val = val[:i] + serversTag + val[i:]
return val, strings.Index(val, endServerTag)
i = loc[1]
}
val = val[:i] + serversTag + val[i:]
return val, strings.Index(val, endServerTag)
}

func injectDependencies(ctx *builderContext) error {
Expand Down
85 changes: 46 additions & 39 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ package cmd
import (
"archive/zip"
"context"

// this is needed to generate an SHA1 sum for Jars
// #nosec G501
"crypto/md5"
// #nosec G505
"crypto/sha1"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -579,11 +583,11 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.C
// TODO: accept URLs
if strings.HasPrefix(item, "file://") {
if platform == nil {
// let's also enable the registry trait if not explicitely disabled
// let's also enable the registry trait if not explicitly disabled
if !contains(o.Traits, "registry.enabled=false") {
o.Traits = append(o.Traits, "registry.enabled=true")
}
platform, err = getPlatform(c, o.Context, integration.Namespace)
platform, err = getPlatform(o.Context, c, integration.Namespace)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -777,7 +781,7 @@ func resolvePodTemplate(ctx context.Context, cmd *cobra.Command, templateSrc str
return err
}

func getPlatform(c client.Client, context context.Context, ns string) (*v1.IntegrationPlatform, error) {
func getPlatform(context context.Context, c client.Client, ns string) (*v1.IntegrationPlatform, error) {
list := v1.NewIntegrationPlatformList()
if err := c.List(context, &list, ctrl.InNamespace(ns)); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("could not retrieve integration platform from namespace %s", ns))
Expand All @@ -794,8 +798,7 @@ func uploadFileOrDirectory(platform *v1.IntegrationPlatform, item string, integr
path := strings.TrimPrefix(item, "file://")
localPath := path
targetPath := ""
i := strings.Index(path, ":")
if i > 0 {
if i := strings.Index(path, ":"); i > 0 {
targetPath = path[i+1:]
localPath = path[:i]
}
Expand All @@ -818,10 +821,11 @@ func uploadFileOrDirectory(platform *v1.IntegrationPlatform, item string, integr
return err
}
// When uploading, there are three cases: POM files, JAR files and the rest which will be mounted on the filesystem
if isPom(path) {
switch {
case isPom(path):
gav := extractGavFromPom(path, gav)
return uploadAsMavenArtifact(gav, path, platform, integration.Namespace, options)
} else if isJar(path) {
case isJar(path):
// Try to upload pom in JAR and extract it's GAV
gav = uploadPomFromJar(gav, path, platform, integration.Namespace, options)
// add JAR to dependency list
Expand All @@ -830,7 +834,7 @@ func uploadFileOrDirectory(platform *v1.IntegrationPlatform, item string, integr
integration.Spec.AddDependency(dependency)
// Upload JAR
return uploadAsMavenArtifact(gav, path, platform, integration.Namespace, options)
} else {
default:
mountPath, err := getMountPath(targetPath, dirName, path)
if err != nil {
return err
Expand All @@ -857,6 +861,7 @@ func getMountPath(targetPath string, dirName string, path string) (string, error
return filepath.Join(targetPath, localRelativePath), nil
}

// nolint:errcheck
func uploadPomFromJar(gav maven.Dependency, path string, platform *v1.IntegrationPlatform, ns string, options spectrum.Options) maven.Dependency {
util.WithTempDir("camel-k", func(tmpDir string) error {
pomPath := filepath.Join(tmpDir, "pom.xml")
Expand All @@ -873,7 +878,7 @@ func uploadPomFromJar(gav maven.Dependency, path string, platform *v1.Integratio
for _, f := range jar.File {
if regPom.MatchString(f.Name) {
foundPom = true
pomExtracted = extractFromZip(pomPath, f, path)
pomExtracted = extractFromZip(pomPath, f)
} else if regPomProperties.MatchString(f.Name) {
foundProperties = true
if dep, ok := extractGav(f, path); ok {
Expand All @@ -895,7 +900,7 @@ func uploadPomFromJar(gav maven.Dependency, path string, platform *v1.Integratio
return gav
}

func extractFromZip(dst string, src *zip.File, tarPath string) bool {
func extractFromZip(dst string, src *zip.File) bool {
file, err := os.Create(dst)
if err != nil {
return false
Expand All @@ -906,11 +911,10 @@ func extractFromZip(dst string, src *zip.File, tarPath string) bool {
return false
}
defer rc.Close()
// no DoS on client side
// #nosec G110
_, err = io.Copy(file, rc)
if err != nil {
return false
}
return true
return err == nil
}

func extractGav(src *zip.File, localPath string) (maven.Dependency, bool) {
Expand All @@ -930,12 +934,12 @@ func extractGav(src *zip.File, localPath string) (maven.Dependency, bool) {
return maven.Dependency{}, false
}

groupId, ok := prop.Get("groupId")
groupID, ok := prop.Get("groupId")
if !ok {
fmt.Printf("Couldn't find groupId property while reading pom.properties from [%s], switching to default \n", localPath)
return maven.Dependency{}, false
}
artifactId, ok := prop.Get("artifactId")
artifactID, ok := prop.Get("artifactId")
if !ok {
fmt.Printf("Couldn't find artifactId property while reading pom.properties from [%s], switching to default \n", localPath)
return maven.Dependency{}, false
Expand All @@ -946,26 +950,27 @@ func extractGav(src *zip.File, localPath string) (maven.Dependency, bool) {
return maven.Dependency{}, false
}
return maven.Dependency{
GroupID: groupId,
ArtifactID: artifactId,
GroupID: groupID,
ArtifactID: artifactID,
Type: "jar",
Version: version,
}, true
}

func uploadAsMavenArtifact(dependency maven.Dependency, path string, platform *v1.IntegrationPlatform, ns string, options spectrum.Options) error {
artifactHttpPath := getArtifactHttpPath(dependency, platform, ns)
options.Target = fmt.Sprintf("%s/%s:%s", platform.Spec.Build.Registry.Address, artifactHttpPath, dependency.Version)
artifactHTTPPath := getArtifactHTTPPath(dependency, platform, ns)
options.Target = fmt.Sprintf("%s/%s:%s", platform.Spec.Build.Registry.Address, artifactHTTPPath, dependency.Version)
_, err := spectrum.Build(options, fmt.Sprintf("%s:.", path))
if err != nil {
return err
}
fmt.Printf("Uploaded: %s to %s \n", path, options.Target)
return uploadChecksumFiles(path, options, platform, artifactHttpPath, dependency)
return uploadChecksumFiles(path, options, platform, artifactHTTPPath, dependency)
}

// Currently swallows errors because our Project model is incomplete
// Most of the time it is irrelevant for our use case (GAV)
// Currently swallows errors because our Project model is incomplete.
// Most of the time it is irrelevant for our use case (GAV).
// nolint:errcheck
func extractGavFromPom(path string, gav maven.Dependency) maven.Dependency {
var project maven.Project
file, err := os.Open(path)
Expand All @@ -991,16 +996,18 @@ func extractGavFromPom(path string, gav maven.Dependency) maven.Dependency {
return gav
}

func uploadChecksumFiles(path string, options spectrum.Options, platform *v1.IntegrationPlatform, artifactHttpPath string, dependency maven.Dependency) error {
func uploadChecksumFiles(path string, options spectrum.Options, platform *v1.IntegrationPlatform, artifactHTTPPath string, dependency maven.Dependency) error {
return util.WithTempDir("camel-k", func(tmpDir string) error {
if err := uploadChecksumFile(md5.New(), tmpDir, "_md5", path, options, platform, artifactHttpPath, dependency); err != nil {
// #nosec G401
if err := uploadChecksumFile(md5.New(), tmpDir, "_md5", path, options, platform, artifactHTTPPath, dependency); err != nil {
return err
}
return uploadChecksumFile(sha1.New(), tmpDir, "_sha1", path, options, platform, artifactHttpPath, dependency)
// #nosec G401
return uploadChecksumFile(sha1.New(), tmpDir, "_sha1", path, options, platform, artifactHTTPPath, dependency)
})
}

func uploadChecksumFile(hash hash.Hash, tmpDir string, ext string, path string, options spectrum.Options, platform *v1.IntegrationPlatform, artifactHttpPath string, dependency maven.Dependency) error {
func uploadChecksumFile(hash hash.Hash, tmpDir string, ext string, path string, options spectrum.Options, platform *v1.IntegrationPlatform, artifactHTTPPath string, dependency maven.Dependency) error {
file, err := os.Open(path)
if err != nil {
return err
Expand All @@ -1017,7 +1024,7 @@ func uploadChecksumFile(hash hash.Hash, tmpDir string, ext string, path string,
if err = writeChecksumToFile(filepath, hash); err != nil {
return err
}
options.Target = fmt.Sprintf("%s/%s%s:%s", platform.Spec.Build.Registry.Address, artifactHttpPath, ext, dependency.Version)
options.Target = fmt.Sprintf("%s/%s%s:%s", platform.Spec.Build.Registry.Address, artifactHTTPPath, ext, dependency.Version)
_, err = spectrum.Build(options, fmt.Sprintf("%s:.", filepath))
return err
}
Expand Down Expand Up @@ -1048,18 +1055,18 @@ func getSpectrumOptions(platform *v1.IntegrationPlatform, cmd *cobra.Command) sp
return options
}

func getArtifactHttpPath(dependency maven.Dependency, platform *v1.IntegrationPlatform, ns string) string {
artifactHttpPath := fmt.Sprintf("maven_%s_%s_%s_%s-%s_%s", dependency.GroupID, dependency.ArtifactID, dependency.Version, dependency.ArtifactID, dependency.Version, dependency.Type)
func getArtifactHTTPPath(dependency maven.Dependency, platform *v1.IntegrationPlatform, ns string) string {
artifactHTTPPath := fmt.Sprintf("maven_%s_%s_%s_%s-%s_%s", dependency.GroupID, dependency.ArtifactID, dependency.Version, dependency.ArtifactID, dependency.Version, dependency.Type)
// Image repository names must be lower cased
artifactHttpPath = strings.ToLower(artifactHttpPath)
artifactHTTPPath = strings.ToLower(artifactHTTPPath)
// Some vendors don't allow '/' or '.' in repository name so let's replace them with '_'
artifactHttpPath = strings.ReplaceAll(artifactHttpPath, "/", "_")
artifactHttpPath = strings.ReplaceAll(artifactHttpPath, ".", "_")
artifactHTTPPath = strings.ReplaceAll(artifactHTTPPath, "/", "_")
artifactHTTPPath = strings.ReplaceAll(artifactHTTPPath, ".", "_")
if platform.Spec.Cluster == v1.IntegrationPlatformClusterOpenShift {
// image must be uploaded in the namespace
artifactHttpPath = fmt.Sprintf("%s/%s", ns, artifactHttpPath)
artifactHTTPPath = fmt.Sprintf("%s/%s", ns, artifactHTTPPath)
}
return artifactHttpPath
return artifactHTTPPath
}

func createDefaultGav(path string, dirName string, integrationName string) (maven.Dependency, error) {
Expand All @@ -1071,13 +1078,13 @@ func createDefaultGav(path string, dirName string, integrationName string) (mave
return maven.Dependency{}, err
}

defaultArtifactId := integrationName + "-" + strings.ReplaceAll(fileRelPath, string(os.PathSeparator), ".")
defaultGroupId := "org.apache.camel.k.external"
defaultArtifactID := integrationName + "-" + strings.ReplaceAll(fileRelPath, string(os.PathSeparator), ".")
defaultGroupID := "org.apache.camel.k.external"
defaultVersion := defaults.Version

return maven.Dependency{
GroupID: defaultGroupId,
ArtifactID: defaultArtifactId,
GroupID: defaultGroupID,
ArtifactID: defaultArtifactID,
Type: ext,
Version: defaultVersion,
}, nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/trait/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import (
)

// The Registry trait sets up Maven to use the Image registry
// as a Maven repository
// as a Maven repository.
//
// +camel-k:trait=registry
// +camel-k:trait=registry.
type registryTrait struct {
BaseTrait `property:",squash"`
}
Expand All @@ -49,7 +49,7 @@ func newRegistryTrait() Trait {
}
}

// InfluencesKit overrides base class method
// InfluencesKit overrides base class method.
func (t *registryTrait) InfluencesKit() bool {
return true
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func extractMavenServerCredentialsFromSecret(registrySecret string, e *Environme
}
config, ok := dockerAuth.Auths[registryAddress]
if !ok {
return v1.Server{}, fmt.Errorf("cannot resolve regsitry address %s in secret %s", registryAddress, registrySecret)
return v1.Server{}, fmt.Errorf("cannot resolve registry address %s in secret %s", registryAddress, registrySecret)
}
username := config.Username
password := config.Password
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/camel/camel_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func addDependencies(project *maven.Project, dependencies []string, catalog *Run
gavString := strings.TrimPrefix(mapping[0], "docker-mvn:")
gav, err := maven.ParseGAV(gavString)
if err != nil {
return nil
return err
}
plugin := getOrCreateBuildPlugin(project, "com.googlecode.maven-download-plugin", "download-maven-plugin", "1.6.7")
exec := maven.Execution{
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func GetSecretRefData(ctx context.Context, client ctrl.Reader, namespace string,
// GetSecretsRefData returns the value of the secrets in the supplied namespace.
func GetSecretsRefData(ctx context.Context, client ctrl.Reader, namespace string, selector []corev1.SecretKeySelector) ([][]byte, error) {
certsData := make([][]byte, len(selector))
for i, secret := range selector {
certData, err := GetSecretRefData(ctx, client, namespace, &secret)
for i := range selector {
certData, err := GetSecretRefData(ctx, client, namespace, &selector[i])
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/util/maven/maven_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ type Execution struct {
// Settings models a Maven settings.
type Settings struct {
XMLName xml.Name
XMLNs string `xml:"xmlns,attr"`
XMLNsXsi string `xml:"xmlns:xsi,attr"`
XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"`
LocalRepository string `xml:"localRepository"`
XMLNs string `xml:"xmlns,attr"`
XMLNsXsi string `xml:"xmlns:xsi,attr"`
XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"`
LocalRepository string `xml:"localRepository"`
Servers []v1.Server `xml:"servers>server,omitempty"`
Profiles []Profile `xml:"profiles>profile,omitempty"`
Proxies []Proxy `xml:"proxies>proxy,omitempty"`
Mirrors []Mirror `xml:"mirrors>mirror,omitempty"`
Profiles []Profile `xml:"profiles>profile,omitempty"`
Proxies []Proxy `xml:"proxies>proxy,omitempty"`
Mirrors []Mirror `xml:"mirrors>mirror,omitempty"`
}

// Project models a Maven project.
Expand Down

0 comments on commit b3f2968

Please sign in to comment.