1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
20 "git.curoverse.com/arvados.git/sdk/go/keepclient"
24 err := doMain(os.Args[1:])
30 func doMain(args []string) error {
31 flags := flag.NewFlagSet("keep-block-check", flag.ExitOnError)
33 configFile := flags.String(
36 "Configuration filename. May be either a pathname to a config file, or (for example) 'foo' as shorthand for $HOME/.config/arvados/foo.conf file. This file is expected to specify the values for ARVADOS_API_TOKEN, ARVADOS_API_HOST, ARVADOS_API_HOST_INSECURE, and ARVADOS_BLOB_SIGNING_KEY for the source.")
38 keepServicesJSON := flags.String(
41 "An optional list of available keepservices. "+
42 "If not provided, this list is obtained from api server configured in config-file.")
44 locatorFile := flags.String(
47 "Filename containing the block hashes to be checked. This is required. "+
48 "This file contains the block hashes one per line.")
50 prefix := flags.String(
53 "Block hash prefix. When a prefix is specified, only hashes listed in the file with this prefix will be checked.")
55 blobSignatureTTLFlag := flags.Duration(
58 "Lifetime of blob permission signatures on the keepservers. If not provided, this will be retrieved from the API server's discovery document.")
60 verbose := flags.Bool(
63 "Log progress of each block verification")
65 // Parse args; omit the first arg which is the command name
68 config, blobSigningKey, err := loadConfig(*configFile)
70 return fmt.Errorf("Error loading configuration from file: %s", err.Error())
73 // get list of block locators to be checked
74 blockLocators, err := getBlockLocators(*locatorFile, *prefix)
76 return fmt.Errorf("Error reading block hashes to be checked from file: %s", err.Error())
80 kc, blobSignatureTTL, err := setupKeepClient(config, *keepServicesJSON, *blobSignatureTTLFlag)
82 return fmt.Errorf("Error configuring keepclient: %s", err.Error())
85 return performKeepBlockCheck(kc, blobSignatureTTL, blobSigningKey, blockLocators, *verbose)
88 type apiConfig struct {
95 // Load config from given file
96 func loadConfig(configFile string) (config apiConfig, blobSigningKey string, err error) {
98 err = errors.New("Client config file not specified")
102 config, blobSigningKey, err = readConfigFromFile(configFile)
106 // Read config from file
107 func readConfigFromFile(filename string) (config apiConfig, blobSigningKey string, err error) {
108 if !strings.Contains(filename, "/") {
109 filename = os.Getenv("HOME") + "/.config/arvados/" + filename + ".conf"
112 content, err := ioutil.ReadFile(filename)
118 lines := strings.Split(string(content), "\n")
119 for _, line := range lines {
124 kv := strings.SplitN(line, "=", 2)
126 key := strings.TrimSpace(kv[0])
127 value := strings.TrimSpace(kv[1])
130 case "ARVADOS_API_TOKEN":
131 config.APIToken = value
132 case "ARVADOS_API_HOST":
133 config.APIHost = value
134 case "ARVADOS_API_HOST_INSECURE":
135 config.APIHostInsecure = arvadosclient.StringBool(value)
136 case "ARVADOS_EXTERNAL_CLIENT":
137 config.ExternalClient = arvadosclient.StringBool(value)
138 case "ARVADOS_BLOB_SIGNING_KEY":
139 blobSigningKey = value
147 // setup keepclient using the config provided
148 func setupKeepClient(config apiConfig, keepServicesJSON string, blobSignatureTTL time.Duration) (kc *keepclient.KeepClient, ttl time.Duration, err error) {
149 arv := arvadosclient.ArvadosClient{
150 ApiToken: config.APIToken,
151 ApiServer: config.APIHost,
152 ApiInsecure: config.APIHostInsecure,
153 Client: &http.Client{Transport: &http.Transport{
154 TLSClientConfig: &tls.Config{InsecureSkipVerify: config.APIHostInsecure}}},
155 External: config.ExternalClient,
158 // If keepServicesJSON is provided, use it instead of service discovery
159 if keepServicesJSON == "" {
160 kc, err = keepclient.MakeKeepClient(&arv)
165 kc = keepclient.New(&arv)
166 err = kc.LoadKeepServicesFromJSON(keepServicesJSON)
172 // Get if blobSignatureTTL is not provided
173 ttl = blobSignatureTTL
174 if blobSignatureTTL == 0 {
175 value, err := arv.Discovery("blobSignatureTtl")
177 ttl = time.Duration(int(value.(float64))) * time.Second
186 // Get list of unique block locators from the given file
187 func getBlockLocators(locatorFile, prefix string) (locators []string, err error) {
188 if locatorFile == "" {
189 err = errors.New("block-hash-file not specified")
193 content, err := ioutil.ReadFile(locatorFile)
198 locatorMap := make(map[string]bool)
199 for _, line := range strings.Split(string(content), "\n") {
200 line = strings.TrimSpace(line)
201 if line == "" || !strings.HasPrefix(line, prefix) || locatorMap[line] {
204 locators = append(locators, line)
205 locatorMap[line] = true
211 // Get block headers from keep. Log any errors.
212 func performKeepBlockCheck(kc *keepclient.KeepClient, blobSignatureTTL time.Duration, blobSigningKey string, blockLocators []string, verbose bool) error {
213 totalBlocks := len(blockLocators)
216 for _, locator := range blockLocators {
219 log.Printf("Verifying block %d of %d: %v", current, totalBlocks, locator)
221 getLocator := locator
222 if blobSigningKey != "" {
223 expiresAt := time.Now().AddDate(0, 0, 1)
224 getLocator = keepclient.SignLocator(locator, kc.Arvados.ApiToken, expiresAt, blobSignatureTTL, []byte(blobSigningKey))
227 _, _, err := kc.Ask(getLocator)
230 log.Printf("Error verifying block %v: %v", locator, err)
234 log.Printf("Verify block totals: %d attempts, %d successes, %d errors", totalBlocks, totalBlocks-notFoundBlocks, notFoundBlocks)
236 if notFoundBlocks > 0 {
237 return fmt.Errorf("Block verification failed for %d out of %d blocks with matching prefix.", notFoundBlocks, totalBlocks)