github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/roughtime/client/client.go (about)

     1  // Copyright (c) 2016, Google Inc. All rights reserved.
     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  // this is an adapted version of the client code to cnonect to cloudproxy.
    16  package main
    17  
    18  import (
    19  	"encoding/json"
    20  	"flag"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"log"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/jlmucb/cloudproxy/go/apps/roughtime/agl_roughtime/config"
    28  
    29  	"github.com/jlmucb/cloudproxy/go/apps/roughtime"
    30  )
    31  
    32  var (
    33  	chainFile    = flag.String("chain-file", "roughtime-chain.json", "The name of a file in which the query chain will be maintained")
    34  	maxChainSize = flag.Int("max-chain-size", 128, "The maximum number of entries to maintain in the chain file")
    35  	serversFile  = flag.String("servers-file", "roughtime-servers.json", "The name of a file that lists trusted Roughtime servers")
    36  	configPath   = flag.String("config", "tao.config", "Path to domain configuration file.")
    37  )
    38  
    39  const (
    40  	// defaultServerQuorum is the default number of overlapping responses
    41  	// that are required to establish the current time.
    42  	defaultServerQuorum = 2
    43  )
    44  
    45  func main() {
    46  	serversData, err := ioutil.ReadFile(*serversFile)
    47  	if err != nil {
    48  		log.Fatal(err)
    49  	}
    50  
    51  	servers, numServersSkipped, err := roughtime.LoadServers(serversData)
    52  	if err != nil {
    53  		log.Fatal(err)
    54  	}
    55  	if numServersSkipped > 0 {
    56  		fmt.Fprintf(os.Stderr, "Ignoring %d unsupported servers\n", numServersSkipped)
    57  	}
    58  
    59  	c, err := roughtime.NewClient(*configPath, "tcp", defaultServerQuorum, servers)
    60  
    61  	// Read existing chain, if one exists
    62  	chain := &config.Chain{}
    63  	chainData, err := ioutil.ReadFile(*chainFile)
    64  	if err == nil {
    65  		if chain, err = roughtime.LoadChain(chainData); err != nil {
    66  			log.Fatal(err)
    67  		}
    68  	} else if !os.IsNotExist(err) {
    69  		log.Fatal(err)
    70  	}
    71  
    72  	chain, err = c.Do(chain)
    73  	if err != nil {
    74  		fmt.Fprintf(os.Stderr, "%s\n", err)
    75  		os.Exit(1)
    76  	}
    77  
    78  	chainBytes, err := json.MarshalIndent(chain, "", "  ")
    79  	if err != nil {
    80  		log.Fatal(err)
    81  	}
    82  
    83  	tempFile, err := ioutil.TempFile(filepath.Dir(*chainFile), filepath.Base(*chainFile))
    84  	if err != nil {
    85  		log.Fatal(err)
    86  	}
    87  	defer tempFile.Close()
    88  
    89  	if _, err := tempFile.Write(chainBytes); err != nil {
    90  		log.Fatal(err)
    91  	}
    92  
    93  	if err := os.Rename(tempFile.Name(), *chainFile); err != nil {
    94  		log.Fatal(err)
    95  	}
    96  }