// getChef builds a new chef client
func getChef(config ChefConfig) (*chef.Client, error) {
	return chef.NewClient(&chef.Config{
		Name:    config.ClientName,
		Key:     config.PemFile,
		BaseURL: config.ChefServer,
	})
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
	config := &chefc.Config{
		Name:    d.Get("client_name").(string),
		Key:     d.Get("private_key_pem").(string),
		BaseURL: d.Get("server_url").(string),
		SkipSSL: d.Get("allow_unverified_ssl").(bool),
		Timeout: 10 * time.Second,
	}

	return chefc.NewClient(config)
}
Exemple #3
0
// Connect sets up a connection to the Chef server and passed back a client.
func Connect(key, node, url string) *chef.Client {
	Log(fmt.Sprintf("create: Connecting to %s with '%s'", url, node), "info")
	client, err := chef.NewClient(&chef.Config{
		Name:    node,
		Key:     key,
		BaseURL: url,
		SkipSSL: true,
	})
	if err != nil {
		Log("create: Error with Chef connection", "info")
		os.Exit(1)
	}
	return client
}
func (c *Config) Client() (*chefGo.Client, error) {
	key, err := ioutil.ReadFile(c.Key)
	if err != nil {
		return nil, err
	}
	config := chefGo.Config{
		Name:    c.Name,
		Key:     string(key),
		BaseURL: c.BaseURL,
		SkipSSL: false,
	}

	return chefGo.NewClient(&config)
}
Exemple #5
0
func getApiClient(clientName, privateKeyPath, chefServerUrl string) chef.Client {
	privateKey := getPrivateKey(privateKeyPath)

	client, err := chef.NewClient(&chef.Config{
		Name:    clientName,
		Key:     privateKey,
		BaseURL: chefServerUrl,
		SkipSSL: true,
	})
	if err != nil {
		fmt.Println("Issue setting up client:", err)
	}
	return *client
}
Exemple #6
0
func main() {
	if len(os.Args) != 2 {
		printAndExit(fmt.Errorf("No argument given"))
	}
	variablePath := os.Args[1]
	bagName, bagItem, keyName, err := parsePath(variablePath)
	if err != nil {
		printAndExit(fmt.Errorf("Path '%s' is invalid", variablePath))
	}

	nodeName := readEnvVar("CHEF_NODE_NAME")
	clientKeyPath := readEnvVar("CHEF_CLIENT_KEY_PATH")
	serverUrl := readEnvVar("CHEF_SERVER_URL")
	decryptionKeyPath := readEnvVar("CHEF_DECRYPTION_KEY_PATH")

	key, err := ioutil.ReadFile(clientKeyPath)
	if err != nil {
		printAndExit(err)
	}

	decryptionKey, err := ioutil.ReadFile(decryptionKeyPath)
	if err != nil {
		printAndExit(err)
	}

	client, err := chef.NewClient(&chef.Config{
		Name:    nodeName,
		Key:     string(key),
		BaseURL: fmt.Sprintf("%s/foo", serverUrl), // /foo is needed here because of how URLs are parsed by go-chef
		SkipSSL: (os.Getenv("CHEF_SKIP_SSL") == "1"),
	})
	if err != nil {
		printAndExit(err)
	}

	item, err := client.DataBags.GetItem(bagName, bagItem)
	if err != nil {
		printAndExit(err)
	}

	encrypted := NewEncryptedDataBagItem(item)

	unencrypted, err := encrypted.DecryptKey(keyName, decryptionKey)
	if err != nil {
		printAndExit(err)
	}

	fmt.Print(unencrypted)
}
Exemple #7
0
func main() {
	// simple arg parsing
	//cookPath := flag.String("cookbook", "c", "Path to cookbook for upload")
	//	flag.Parse()

	// read a client key
	key, err := ioutil.ReadFile("key.pem")
	if err != nil {
		fmt.Println("Couldn't read key.pem:", err)
		os.Exit(1)
	}

	// build a client
	client, err := chef.NewClient(&chef.Config{
		Name: "foo",
		Key:  string(key),
		// goiardi is on port 4545 by default. chef-zero is 8889
		BaseURL: "http://localhost:8443",
	})
	if err != nil {
		fmt.Println("Issue setting up client:", err)
		os.Exit(1)
	}

	// List Cookbooks
	cookList, err := client.Cookbooks.List()
	if err != nil {
		fmt.Println("Issue listing cookbooks:", err)
	}

	// Print out the list
	fmt.Println(cookList)
	/*
			*'parse' metadata...
		   this would prefer .json over .rb
		    if it's .rb lets maybe try to eval it ?
		      otherwise just extract name/version if they exist
	*/

	/*


		  	* generate sums
				* create sandbox
				* upload to sandbox
				*
	*/

}
Exemple #8
0
func main() {
	// read a client key
	key, err := ioutil.ReadFile("key.pem")
	if err != nil {
		fmt.Println("Couldn't read key.pem:", err)
		os.Exit(1)
	}

	// build a client
	client, err := chef.NewClient(&chef.Config{
		Name: "foo",
		Key:  string(key),
		// goiardi is on port 4545 by default. chef-zero is 8889
		BaseURL: "http://localhost:4545",
	})
	if err != nil {
		fmt.Println("Issue setting up client:", err)
		os.Exit(1)
	}

	// List Indexes
	indexes, err := client.Search.Indexes()
	if err != nil {
		log.Fatal("Couldn't list nodes: ", err)
	}

	// dump the Index list in Json
	jsonData, err := json.MarshalIndent(indexes, "", "\t")
	os.Stdout.Write(jsonData)
	os.Stdout.WriteString("\n")

	// build a seach query
	query, err := client.Search.NewQuery("node", "name:*")
	if err != nil {
		log.Fatal("Error building query ", err)
	}

	// Run the query
	res, err := query.Do(client)
	if err != nil {
		log.Fatal("Error running query ", err)
	}

	// <3 spew
	spew.Dump(res)

	// dump out results back in json for fun
	jsonData, err = json.MarshalIndent(res, "", "\t")
	os.Stdout.Write(jsonData)
	os.Stdout.WriteString("\n")

	// You can also use the service to run a query
	res, err = client.Search.Exec("node", "name:*")
	if err != nil {
		log.Fatal("Error running Search.Exec() ", err)
	}

	// dump out results back in json for fun
	jsonData, err = json.MarshalIndent(res, "", "\t")
	os.Stdout.Write(jsonData)
	os.Stdout.WriteString("\n")

	// Partial search
	log.Print("Partial Search")
	part := make(map[string]interface{})
	part["name"] = []string{"name"}
	pres, err := client.Search.PartialExec("node", "*:*", part)
	if err != nil {
		log.Fatal("Error running Search.PartialExec()", err)
	}

	jsonData, err = json.MarshalIndent(pres, "", "\t")
	os.Stdout.Write(jsonData)
	os.Stdout.WriteString("\n")

}
Exemple #9
0
func main() {
	// read a client key
	key, err := ioutil.ReadFile("key.pem")
	if err != nil {
		fmt.Println("Couldn't read key.pem:", err)
		os.Exit(1)
	}

	// build a client
	client, err := chef.NewClient(&chef.Config{
		Name: "foo",
		Key:  string(key),
		// goiardi is on port 4545 by default. chef-zero is 8889
		BaseURL: "http://localhost:4545",
	})
	if err != nil {
		fmt.Println("Issue setting up client:", err)
		os.Exit(1)
	}

	// Create a Node object
	// TOOD: should have a constructor for this
	ranjib := chef.Node{
		Name:        "ranjib",
		Environment: "_default",
		ChefType:    "node",
		JsonClass:   "Chef::Node",
		RunList:     []string{"pwn"},
	}

	// Delete node ignoring errors :)
	client.Nodes.Delete(ranjib.Name)

	// Create
	_, err = client.Nodes.Post(ranjib)
	if err != nil {
		log.Fatal("Couldn't create node. ", err)
	}

	// List nodes
	nodeList, err := client.Nodes.List()
	if err != nil {
		log.Fatal("Couldn't list nodes: ", err)
	}

	// dump the node list in Json
	jsonData, err := json.MarshalIndent(nodeList, "", "\t")
	os.Stdout.Write(jsonData)
	os.Stdout.WriteString("\n")

	// dump the ranjib node we got from server in JSON!
	serverNode, _ := client.Nodes.Get("ranjib")
	if err != nil {
		log.Fatal("Couldn't get node: ", err)
	}
	jsonData, err = json.MarshalIndent(serverNode, "", "\t")
	os.Stdout.Write(jsonData)
	os.Stdout.WriteString("\n")

	// update node
	ranjib.RunList = append(ranjib.RunList, "recipe[works]")
	jsonData, err = json.MarshalIndent(ranjib, "", "\t")
	os.Stdout.Write(jsonData)
	os.Stdout.WriteString("\n")

	_, err = client.Nodes.Put(ranjib)
	if err != nil {
		log.Fatal("Couldn't update node: ", err)
	}

	// Delete node ignoring errors :)
	client.Nodes.Delete(ranjib.Name)

}
Exemple #10
0
func main() {
	// read a client key
	key, err := ioutil.ReadFile("key.pem")
	if err != nil {
		fmt.Println("Couldn't read key.pem:", err)
		os.Exit(1)
	}

	// build a client
	client, err := chef.NewClient(&chef.Config{
		Name: "foo",
		Key:  string(key),
		// goiardi is on port 4545 by default. chef-zero is 8889
		BaseURL: "http://localhost:4545",
	})
	if err != nil {
		fmt.Println("Issue setting up client:", err)
		os.Exit(1)
	}

	// create junk files and sums
	files := make(map[string][]byte)
	sums := make([]string, 10)
	for i := 0; i < 10; i++ {
		data := random_data(1024)
		hashstr := fmt.Sprintf("%x", md5.Sum(data))
		files[hashstr] = data
		sums[i] = hashstr
	}

	// post the new sums/files to the sandbox
	postResp, err := client.Sandboxes.Post(sums)
	if err != nil {
		fmt.Println("error making request: ", err)
		os.Exit(1)
	}

	// Dump the the server response data
	j, err := json.MarshalIndent(postResp, "", "    ")
	fmt.Printf("%s", j)

	// Let's upload the files that postRep thinks we should upload
	for hash, item := range postResp.Checksums {
		if item.Upload == true {
			if hash == "" {
				continue
			}
			// If you were writing this in your own tool you could just use the FH and let the Reader interface suck out the content instead of doing the convert.
			fmt.Printf("\nUploading: %s --->  %v\n\n", hash, item)
			req, err := client.NewRequest("PUT", item.Url, bytes.NewReader(files[hash]))
			if err != nil {
				fmt.Println("This shouldn't happen:", err.Error())
				os.Exit(1)
			}

			// post the files
			upload := func() error {
				_, err = client.Do(req, nil)
				return err
			}

			// with exp backoff !
			err = backoff.Retry(upload, backoff.NewExponentialBackOff())
			if err != nil {
				fmt.Println("error posting files to the sandbox: ", err.Error())
			}
		}
	}

	// Now lets tell the server we have uploaded all the things.
	sandbox, err := client.Sandboxes.Put(postResp.ID)
	if err != nil {
		fmt.Println("Error commiting sandbox: ", err.Error())
		os.Exit(1)
	}

	// and heres yer commited sandbox
	spew.Dump(sandbox)

}