1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/mendersoftware/mender-artifact/areader"
"github.com/mendersoftware/mender-artifact/artifact"
"github.com/mendersoftware/mender-artifact/handlers"
"github.com/mendersoftware/mender-artifact/utils"
)
func readArtifact(c *cli.Context) error {
if c.NArg() == 0 {
return cli.NewExitError("Nothing specified, nothing read. \nMaybe you wanted"+
" to say 'artifacts read <pathspec>'?", errArtifactInvalidParameters)
}
f, err := os.Open(c.Args().First())
if err != nil {
return cli.NewExitError("Can not open artifact: "+c.Args().First(),
errArtifactOpen)
}
defer f.Close()
var verifyCallback areader.SignatureVerifyFn
key, err := getKey(c)
if err != nil {
return cli.NewExitError(err.Error(), errArtifactInvalidParameters)
}
if key != nil {
verifyCallback = key.Verify
}
// if key is not provided just continue reading artifact returning
// info that signature can not be verified
sigInfo := "no signature"
ver := func(message, sig []byte) error {
sigInfo = "signed but no key for verification provided; " +
"please use `-k` option for providing verification key"
if key != nil {
err = verifyCallback(message, sig)
if err != nil {
sigInfo = "signed; verification using provided key failed"
} else {
sigInfo = "signed and verified correctly"
}
}
return nil
}
var scripts []string
readScripts := func(r io.Reader, info os.FileInfo) error {
scripts = append(scripts, info.Name())
return nil
}
ar := areader.NewReader(f)
if !c.Bool("no-progress") {
fmt.Fprintln(os.Stderr, "Reading Artifact...")
ar.ProgressReader = utils.NewProgressReader()
}
ar.ScriptsReadCallback = readScripts
ar.VerifySignatureCallback = ver
err = ar.ReadArtifact()
if err != nil {
if errors.Cause(err) == artifact.ErrCompatibleDevices {
return cli.NewExitError("Invalid Artifact. No 'device-type' found.", 1)
}
return cli.NewExitError(err.Error(), 1)
}
inst := ar.GetHandlers()
info := ar.GetInfo()
fmt.Printf("Mender artifact:\n")
fmt.Printf(" Name: %s\n", ar.GetArtifactName())
fmt.Printf(" Format: %s\n", info.Format)
fmt.Printf(" Version: %d\n", info.Version)
fmt.Printf(" Signature: %s\n", sigInfo)
fmt.Printf(" Compatible devices: '%s'\n", ar.GetCompatibleDevices())
provides := ar.GetArtifactProvides()
if provides != nil {
fmt.Printf(" Provides group: %s\n", provides.ArtifactGroup)
}
depends := ar.GetArtifactDepends()
if depends != nil {
fmt.Printf(
" Depends on one of artifact(s): [%s]\n",
strings.Join(depends.ArtifactName, ", "),
)
fmt.Printf(
" Depends on one of group(s): [%s]\n",
strings.Join(depends.ArtifactGroup, ", "),
)
}
if len(scripts) > -1 {
fmt.Printf(" State scripts:\n")
}
for _, scr := range scripts {
fmt.Printf(" %s\n", scr)
}
fmt.Printf("\nUpdates:\n")
for k, p := range inst {
printPayload(k, p)
}
return nil
}
func sortedKeys(mapWithKeys interface{}) sort.StringSlice {
var keys sort.StringSlice
mapVal := reflect.ValueOf(mapWithKeys)
if mapVal.Kind() != reflect.Map {
return nil
}
keys = make([]string, mapVal.Len())
keysVal := mapVal.MapKeys()
for i, keyVal := range keysVal {
keys[i] = keyVal.String()
}
keys.Sort()
return keys
}
func printPayload(index int, p handlers.Installer) {
fmt.Printf(" %3d:\n", index)
updateType := p.GetUpdateType()
if updateType == nil {
emptyType := "Empty type"
updateType = &emptyType
}
fmt.Printf(" Type: %v\n", *updateType)
provides, err := p.GetUpdateProvides()
fmt.Printf(" Provides:")
if err != nil {
fmt.Printf(" Invalid provides section: %s\n", err.Error())
} else if provides == nil || len(provides) == 0 {
fmt.Printf(" Nothing\n")
} else {
providesKeys := sortedKeys(provides)
fmt.Printf("\n")
for _, provideKey := range providesKeys {
fmt.Printf("\t%s: %s\n", provideKey, (provides)[provideKey])
}
}
depends, err := p.GetUpdateDepends()
fmt.Printf(" Depends:")
if err != nil {
fmt.Printf(" Invalid depends section: %s\n", err.Error())
} else if depends == nil || len(depends) == 0 {
fmt.Printf(" Nothing\n")
} else {
dependsKeys := sortedKeys(depends)
fmt.Printf("\n")
for _, dependKey := range dependsKeys {
fmt.Printf("\t%s: %s\n", dependKey, (depends)[dependKey])
}
}
caps := p.GetUpdateClearsProvides()
if caps != nil {
fmt.Printf(" Clears Provides: [\"%s\"]\n", strings.Join(caps, "\", \""))
}
metaData, err := p.GetUpdateMetaData()
fmt.Printf(" Metadata:")
if err != nil {
fmt.Printf(" Invalid metadata section: %s\n", err.Error())
} else if len(metaData) == 0 {
fmt.Printf(" Nothing\n")
} else {
var metaDataSlice []byte
if err == nil {
metaDataSlice, err = json.Marshal(metaData)
}
var metaDataBuf bytes.Buffer
if err == nil {
err = json.Indent(&metaDataBuf, metaDataSlice, "\t", " ")
}
if err != nil {
fmt.Printf(" Invalid metadata section: %s\n", err.Error())
} else {
fmt.Printf("\n\t%s\n", metaDataBuf.String())
}
}
if len(p.GetUpdateAllFiles()) == 0 {
fmt.Printf(" Files: None\n")
} else {
fmt.Printf(" Files:\n")
for _, f := range p.GetUpdateAllFiles() {
fmt.Printf(" name: %s\n", f.Name)
fmt.Printf(" size: %d\n", f.Size)
fmt.Printf(" modified: %s\n", f.Date)
fmt.Printf(" checksum: %s\n", f.Checksum)
}
}
}
|