• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-artifact / 1612715840

06 Jan 2025 09:59AM UTC coverage: 77.109% (-0.02%) from 77.125%
1612715840

Pull #659

gitlab-ci

bahaa-ghazal
fix: fixed singing artifact via symlink

Changelog: Title
Ticket: MEN-3410
Signed-off-by: Bahaa Aldeen Ghazal <bahaa.ghazal@northern.tech>
Pull Request #659: fix: fixed singing artifact via symlink

8 of 14 new or added lines in 1 file covered. (57.14%)

3 existing lines in 1 file now uncovered.

5804 of 7527 relevant lines covered (77.11%)

134.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/cli/sign.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package cli
16

17
import (
18
        "io/ioutil"
19
        "os"
20
        "path/filepath"
21

22
        "github.com/pkg/errors"
23
        "github.com/urfave/cli"
24

25
        "github.com/mendersoftware/mender-artifact/awriter"
26
)
27

28
func signExisting(c *cli.Context) error {
23✔
29
        if c.NArg() == 0 {
23✔
30
                return cli.NewExitError("Nothing specified, nothing signed. \nMaybe you wanted"+
×
31
                        " to say 'artifacts sign <pathspec>'?", 1)
×
32
        }
×
33

34
        privateKey, err := getKey(c)
23✔
35
        if err != nil {
25✔
36
                return cli.NewExitError("Can not use signing key provided: "+err.Error(), 1)
2✔
37
        }
2✔
38
        if privateKey == nil {
23✔
39
                return cli.NewExitError("Missing signing key; "+
2✔
40
                        "please provide a signing key parameter", 1)
2✔
41
        }
2✔
42

43
        artFile := c.Args().First()
19✔
44
        outputFile := artFile
19✔
45
        if len(c.String("output-path")) > 0 {
35✔
46
                outputFile = c.String("output-path")
16✔
47
        }
16✔
48
        tFile, err := ioutil.TempFile(filepath.Dir(outputFile), "mender-artifact")
19✔
49
        if err != nil {
19✔
50
                err = errors.Wrap(err, "Can not create temporary file for storing artifact")
×
51
                return cli.NewExitError(err, 1)
×
52
        }
×
53
        defer os.Remove(tFile.Name())
19✔
54
        defer tFile.Close()
19✔
55

19✔
56
        artFileStat, err := os.Lstat(artFile)
19✔
57
        if err != nil {
19✔
NEW
58
                return cli.NewExitError("Could not get artifact file stat", 1)
×
NEW
59
        }
×
60

61
        if artFileStat.Mode()&os.ModeSymlink == os.ModeSymlink {
21✔
62
                artFile, err = os.Readlink(artFile)
2✔
63
                if err != nil {
2✔
NEW
64
                        return cli.NewExitError(err, 1)
×
NEW
65
                }
×
66
                outputFile = artFile
2✔
67
                artFileStat, err = os.Stat(artFile)
2✔
68
                if err != nil {
2✔
NEW
69
                        return cli.NewExitError("Could not get artifact file stat", 1)
×
NEW
70
                }
×
71

72
        }
73
        f, err := os.Open(artFile)
19✔
74
        if err != nil {
19✔
75
                err = errors.Wrapf(err, "Can not open: %s", artFile)
×
76
                return cli.NewExitError(err, 1)
×
77
        }
×
78
        defer f.Close()
19✔
79
        err = CopyOwner(tFile, artFile)
19✔
80
        if err != nil {
19✔
81
                return cli.NewExitError("Could not set owner/group of signed artifact "+
×
82
                        "(needs root privileges)", 1)
×
83
        }
×
84
        err = os.Chmod(tFile.Name(), artFileStat.Mode())
19✔
85
        if err != nil {
19✔
86
                return cli.NewExitError("Could not give signed artifact same permissions", 1)
×
87
        }
×
88
        err = awriter.SignExisting(f, tFile, privateKey, c.Bool("force"))
19✔
89
        if err == awriter.ErrAlreadyExistingSignature {
21✔
90
                return cli.NewExitError(
2✔
91
                        "Artifact already signed, refusing to re-sign. Use force option to override",
2✔
92
                        1,
2✔
93
                )
2✔
94
        } else if err != nil {
23✔
95
                return cli.NewExitError(err, 1)
4✔
96
        }
4✔
97

98
        if err = tFile.Close(); err != nil {
13✔
99
                return err
×
100
        }
×
101

102
        err = os.Rename(tFile.Name(), outputFile)
13✔
103
        if err != nil {
13✔
104
                return cli.NewExitError("Can not store signed artifact: "+err.Error(), 1)
×
105
        }
×
106
        return nil
13✔
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc