Beispiel #1
1
func IsLeaderAlive(currentLeader string) {

	if retryCount > 0 {
		fmt.Println("Trying......", retryCount)
		transport := http.Transport{
			Dial: dialTimeout,
		}

		client := http.Client{
			Transport: &transport,
		}

		url := fmt.Sprintf("http://localhost:%s/isalive", currentLeader)
		res, err := client.Get(url)

		if err != nil {

			time.Sleep(5000 * time.Millisecond)
			retryCount--
			IsLeaderAlive(currentLeader)
		}

		fmt.Println("Response Status:", res.StatusCode)
		if res.StatusCode == 200 {
			time.Sleep(5000 * time.Millisecond)
			IsLeaderAlive(currentLeader)
		}
	} else {
		resp, _ := http.Get("http://localhost:9999/flushall")
		resp.Body.Close()
		initiateElection()
	}
}
Beispiel #2
1
func getAppID(client *http.Client, url string) (string, error) {
	// Generate a pseudo-random token for handshaking.
	token := strconv.Itoa(rand.New(rand.NewSource(time.Now().UnixNano())).Int())

	resp, err := client.Get(fmt.Sprintf("%s?rtok=%s", url, token))
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("bad response %d; body: %q", resp.StatusCode, body)
	}
	if err != nil {
		return "", fmt.Errorf("failed reading response: %v", err)
	}

	// Check the token is present in response.
	if !bytes.Contains(body, []byte(token)) {
		return "", fmt.Errorf("token not found: want %q; body %q", token, body)
	}

	match := appIDRE.FindSubmatch(body)
	if match == nil {
		return "", fmt.Errorf("app ID not found: body %q", body)
	}

	return string(match[1]), nil
}
Beispiel #3
1
func (self *ServerSuite) TestSslSupport(c *C) {
	data := `
  [{
    "points": [
        ["val1", 2]
    ],
    "name": "test_sll",
    "columns": ["val_1", "val_2"]
  }]`
	self.serverProcesses[0].Post("/db/test_rep/series?u=paul&p=pass", data, c)

	encodedQuery := url.QueryEscape("select * from test_sll")
	fullUrl := fmt.Sprintf("https://localhost:60503/db/test_rep/series?u=paul&p=pass&q=%s", encodedQuery)
	transport := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := http.Client{
		Transport: transport,
	}
	resp, err := client.Get(fullUrl)
	c.Assert(err, IsNil)
	defer resp.Body.Close()
	c.Assert(resp.StatusCode, Equals, http.StatusOK)
	body, err := ioutil.ReadAll(resp.Body)
	c.Assert(err, IsNil)
	var js []*common.SerializedSeries
	err = json.Unmarshal(body, &js)
	c.Assert(err, IsNil)
	collection := ResultsToSeriesCollection(js)
	series := collection.GetSeries("test_sll", c)
	c.Assert(len(series.Points) > 0, Equals, true)
}
func getQuote(symbol string) float64 {
	// set http timeout
	client := http.Client{Timeout: timeout}

	url := fmt.Sprintf("http://finance.yahoo.com/webservice/v1/symbols/%s/quote?format=json", symbol)
	res, err := client.Get(url)
	if err != nil {
		fmt.Errorf("Stocks cannot access the yahoo finance API: %v", err)
	}
	defer res.Body.Close()

	content, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Errorf("Stocks cannot read json body: %v", err)
	}

	var stock Stock

	err = json.Unmarshal(content, &stock)
	if err != nil {
		fmt.Errorf("Stocks cannot parse the json data: %v", err)
	}

	price, err := strconv.ParseFloat(stock.List.Resources[0].Resource.Fields.Price, 64)
	if err != nil {
		fmt.Errorf("Stock price: %v", err)
	}

	return price
}
Beispiel #5
0
func main() {
	baseURL := "http://axe-level-1.herokuapp.com/lv3"
	firstPage := true

	jar, err := cookiejar.New(nil)
	if err != nil {
		fmt.Println("cannot create cookiejar!?")
		return
	}
	client := http.Client{Jar: jar}

	towns := make([]*AreaLeader, 0, 1024)
	for {
		var resp *http.Response
		if resp, err = client.Get(baseURL); err != nil {
			fmt.Println("cannot get page!?")
			return
		}
		if firstPage {
			firstPage = false
			baseURL = baseURL + "?page=next"
		}
		newTowns, hasNext := parsePage(resp)

		towns = append(towns, newTowns...)
		if !hasNext {
			break
		}
	}
	//fmt.Println("len(towns)", len(towns))

	if err := json.NewEncoder(os.Stdout).Encode(towns); err != nil {
		fmt.Println(err)
	}
}
Beispiel #6
0
func (clair ClairAPI) GetVersions() (versions GetVersionsAnswer, err error) {
	var client *http.Client
	url := clair.Address + ":" + strconv.Itoa(clair.Port) + "/v1/versions"
	if clair.HttpsEnable {
		url = "https://" + url
		client = tlsClient
	} else {
		url = "http://" + url
		client = httpClient
	}

	response, err := client.Get(url)
	if err != nil {
		log.Println("Send request failed request: ", err)
		return
	}

	defer response.Body.Close()
	body, _ := ioutil.ReadAll(response.Body)

	// if OK  - returned "200 OK"
	if response.StatusCode != 200 {
		log.Println("Error - response not ok - ", response.Status, " with message: ", string(body))
		return versions, errors.New(string(body))
	}

	err = json.Unmarshal(body, &versions)
	if err != nil {
		log.Println("Cannot parse answer from clair to json: ", err)
		return
	}
	return
}
Beispiel #7
0
func runQuery(t *testing.T, expected int, shouldErr bool, wg *sync.WaitGroup) {
	wg.Add(1)
	defer wg.Done()
	client := http.Client{}
	r, err := client.Get("http://localhost:3000")
	if shouldErr && err == nil {
		t.Fatal("Expected an error but none was encountered.")
	} else if shouldErr && err != nil {
		if err.(*url.Error).Err == io.EOF {
			return
		}
		errno := err.(*url.Error).Err.(*net.OpError).Err.(syscall.Errno)
		if errno == syscall.ECONNREFUSED {
			return
		} else if err != nil {
			t.Fatal("Error on Get:", err)
		}
	}

	if r != nil && r.StatusCode != expected {
		t.Fatalf("Incorrect status code on response. Expected %d. Got %d", expected, r.StatusCode)
	} else if r == nil {
		t.Fatal("No response when a response was expected.")
	}
}
Beispiel #8
0
// LongPollDelta waits for a notification to happen.
func (db *Dropbox) LongPollDelta(cursor string, timeout int) (*DeltaPoll, error) {
	var rv DeltaPoll
	var params *url.Values
	var body []byte
	var rawurl string
	var response *http.Response
	var err error
	var client http.Client

	params = &url.Values{}
	if timeout != 0 {
		if timeout < PollMinTimeout || timeout > PollMaxTimeout {
			return nil, fmt.Errorf("timeout out of range [%d; %d]", PollMinTimeout, PollMaxTimeout)
		}
		params.Set("timeout", strconv.FormatInt(int64(timeout), 10))
	}
	params.Set("cursor", cursor)
	rawurl = fmt.Sprintf("%s/longpoll_delta?%s", db.APINotifyURL, params.Encode())
	if response, err = client.Get(rawurl); err != nil {
		return nil, err
	}
	defer response.Body.Close()
	if body, err = getResponse(response); err != nil {
		return nil, err
	}
	err = json.Unmarshal(body, &rv)
	return &rv, err
}
Beispiel #9
0
// WaitHTTP waits until http available
func (c *Container) WaitHTTP(port int, path string, timeout time.Duration) int {
	p := c.ports[port]
	if p == 0 {
		log.Fatalf("port %d is not exposed on %s", port, c.image)
	}
	now := time.Now()
	end := now.Add(timeout)
	for {
		cli := http.Client{Timeout: timeout}
		res, err := cli.Get("http://" + c.Addr(port) + path)
		if err != nil {
			if time.Now().After(end) {
				log.Fatalf("http not available on port %d for %s err:%v", port, c.image, err)
			}
			// sleep 1 sec to retry
			time.Sleep(1 * time.Second)
			continue
		}
		defer res.Body.Close()
		if res.StatusCode < 200 || res.StatusCode >= 300 {
			if time.Now().After(end) {
				log.Fatalf("http has not valid status code on port %d for %s code:%d", port, c.image, res.StatusCode)
			}
			// sleep 1 sec to retry
			time.Sleep(1 * time.Second)
			continue
		}
		break
	}
	return p
}
Beispiel #10
0
func submitVded(host string, metricName string, value int64) (e error) {
	if *vdedServer == "" {
		return errors.New("No VDED server, cannot continue")
	}

	url := fmt.Sprintf("http://%s:48333/vector?host=%s&vector=delta_%s&value=%d&ts=%d&submit_metric=1", *vdedServer, host, metricName, value, time.Now().Unix())
	log.Debug("VDED: " + url)

	c := http.Client{
		Transport: &http.Transport{
			Dial: timeoutDialer(time.Duration(5) * time.Second),
		},
	}

	resp, err := c.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	log.Debug("VDED returned: " + string(body))

	return
}
Beispiel #11
0
func retrieveQueue(client *http.Client, uri string, queue chan string) {
	fmt.Println("fetching: " + uri + " ...")
	var added = make(map[string]bool)
	resp, err := client.Get(uri)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	added[uri] = true
	// fmt.Println("uri visited is: ", uri)
	// fmt.Println("type of uri: ", reflect.TypeOf(uri))

	links := collectlinks.All(resp.Body)
	for _, link := range links {
		absolute := fixURL(link, uri)
		// fmt.Println("ab visited is: ", absolute)
		if absolute != "" && !added[absolute] {
			// fmt.Println("type of ab: ", reflect.TypeOf(absolute))
			added[absolute] = true
			go func() { queue <- absolute }()
		}
	}

}
Beispiel #12
0
func Example_serviceAccounts() {
	// Your credentials should be obtained from the Google
	// Developer Console (https://console.developers.google.com).
	config, err := google.NewServiceAccountConfig(&oauth2.JWTOptions{
		Email: "*****@*****.**",
		// PEMFilename. If you have a p12 file instead, you
		// can use `openssl` to export the private key into a pem file.
		// $ openssl pkcs12 -in key.p12 -out key.pem -nodes
		PEMFilename: "/path/to/pem/file.pem",
		Scopes: []string{
			"https://www.googleapis.com/auth/bigquery",
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	// Initiate an http.Client, the following GET request will be
	// authorized and authenticated on the behalf of
	// [email protected].
	client := http.Client{Transport: config.NewTransport()}
	client.Get("...")

	// If you would like to impersonate a user, you can
	// create a transport with a subject. The following GET
	// request will be made on the behalf of [email protected].
	client = http.Client{Transport: config.NewTransportWithUser("*****@*****.**")}
	client.Get("...")
}
Beispiel #13
0
func Example_webServer() {
	// Your credentials should be obtained from the Google
	// Developer Console (https://console.developers.google.com).
	config, err := google.NewConfig(&oauth2.Options{
		ClientID:     "YOUR_CLIENT_ID",
		ClientSecret: "YOUR_CLIENT_SECRET",
		RedirectURL:  "YOUR_REDIRECT_URL",
		Scopes: []string{
			"https://www.googleapis.com/auth/bigquery",
			"https://www.googleapis.com/auth/blogger"},
	})
	if err != nil {
		log.Fatal(err)
	}

	// Redirect user to Google's consent page to ask for permission
	// for the scopes specified above.
	url := config.AuthCodeURL("")
	fmt.Printf("Visit the URL for the auth dialog: %v", url)

	// Handle the exchange code to initiate a transport
	t, err := config.NewTransportWithCode("exchange-code")
	if err != nil {
		log.Fatal(err)
	}
	client := http.Client{Transport: t}
	client.Get("...")
}
Beispiel #14
0
// Ping pings the host,
// it returns the response HTTP status code, body and error
func (h Host) Ping(c *http.Client) (int, []byte, error) {
	res, err := c.Get(h.Url)
	if err != nil {
		return 0, nil, err
	}

	defer res.Body.Close()

	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return 0, nil, err
	}

	// compare the body
	if h.ExpectedBody != "" && string(b) != h.ExpectedBody {
		err = ErrBodyMismatch
	}

	// compare the status code
	if h.ExpectedStatusCode != 0 && h.ExpectedStatusCode != res.StatusCode {
		err = ErrStatusCodeMismatch
	} else if h.ExpectedStatusCode == 0 && res.StatusCode != 200 {
		err = ErrBadStatusCode
	}

	return res.StatusCode, b, err
}
// get performs an HTTPS GET to the specified path for a specific node.
func get(t *testing.T, client *http.Client, node *localcluster.Container, path string) []byte {
	url := fmt.Sprintf("https://%s%s", node.Addr(""), path)
	// There seems to be some issues while trying to connect to the status
	// server, so retry (up to 5 times) with a 1 second delay each time.
	// TODO(Bram): Clean this up once we get to the bottom of the issue.
	for r := retry.Start(retryOptions); r.Next(); {
		resp, err := client.Get(url)
		if err != nil {
			t.Logf("could not GET %s - %s", url, err)
			continue
		}
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Logf("could not open body for %s - %s", url, err)
			continue
		}
		defer resp.Body.Close()
		if resp.StatusCode != http.StatusOK {
			t.Logf("could not GET %s - statuscode: %d - body: %s", url, resp.StatusCode, body)
			continue
		}
		t.Logf("OK response from %s", url)
		return body
	}
	t.Fatalf("There was an error retrieving %s", url)
	return []byte("")
}
Beispiel #16
0
// <input type=hidden name=submitkey value="c0a9e3dfa16a586e2ddfd2dfe9bcc002">
func GetSubmitkey(client *http.Client) (string, string, error) {
	debug := ""

	if resp, err := client.Get("https://" + bksvHost + bksvPath + "?json=1"); err != nil {
		return debug, "", err

	} else {
		if false {
			defer resp.Body.Close()
			body, _ := ioutil.ReadAll(resp.Body)
			out := regexp.MustCompile(`"submitKey":"([^"]+)"`).FindStringSubmatch(string(body))
			if out == nil {
				debug += fmt.Sprintf("GetSubmitKey: regexp failued lookup: %v\n")
				return debug, "", fmt.Errorf("GetSubmitKey/regexp/none found")
			}
			return debug, out[1], nil

		} else {
			var jsonMap map[string]interface{}

			err = json.NewDecoder(resp.Body).Decode(&jsonMap)
			if v := jsonMap["submitKey"]; v == nil {
				debug += fmt.Sprintf("GetSubmitKey: Jsonresponse was empty: %v\n", jsonMap)
				return debug, "", fmt.Errorf("GetSubmitKey/NoJsonField")

			} else if submitkey := v.(string); submitkey == "" {
				return debug, "", fmt.Errorf("GetSubmitKey/init response did not have a submitkey")

			} else {
				return debug, submitkey, nil
			}
		}
	}
}
Beispiel #17
0
func (self *Server) WaitForServerToStart() {
	url := fmt.Sprintf("http://localhost:%d/ping", self.apiPort)
	if self.sslOnly {
		url = fmt.Sprintf("https://localhost:%d/ping", self.sslApiPort)
	}

	client := http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		},
	}

	for i := 0; i < 600; i++ {
		resp, err := client.Get(url)
		if err != nil {
			// wait 100 milliseconds before retrying to give the server a
			// chance to startup
			time.Sleep(100 * time.Millisecond)
			continue
		}
		defer resp.Body.Close()
		if resp.StatusCode != http.StatusOK {
			panic(resp)
		}
		// wait for the server to be marked up by other servers
		time.Sleep(500 * time.Millisecond)
		return
	}
	// wait for other servers to send heartbeat and detect that the
	// server is up
	panic("server didn't start")
}
Beispiel #18
0
// Read all members of an etcd cluster
func etcdMembers(url string, client *http.Client) (error, *members) {
	resp, err := client.Get(url + "/v2/members")
	if err != nil {
		return err, nil
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return errors.New("unsuccesful http status code"), nil
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err, nil
	}

	members := &members{}

	err = json.Unmarshal(body, &members)
	if err != nil {
		return err, nil
	}

	return nil, members
}
Beispiel #19
0
func getLeaderStatus(tr *http.Transport, endpoints []string) (string, raftStatus, error) {
	// TODO: use new etcd client
	httpclient := http.Client{
		Transport: tr,
	}

	for _, ep := range endpoints {
		resp, err := httpclient.Get(ep + "/debug/vars")
		if err != nil {
			continue
		}
		defer resp.Body.Close()
		if resp.StatusCode != http.StatusOK {
			continue
		}

		vs := &vars{}
		d := json.NewDecoder(resp.Body)
		err = d.Decode(vs)
		if err != nil {
			continue
		}
		if vs.RaftStatus.Lead != vs.RaftStatus.ID {
			continue
		}
		return ep, vs.RaftStatus, nil
	}
	return "", raftStatus{}, errors.New("no leader")
}
Beispiel #20
0
/*
 * Here we're not really testing the API functionality (we can't, since it
 * expects us to be inside a container to work), but it is useful to test that
 * all the grotty connection extracting stuff works (that is, it gets to the
 * point where it realizes the pid isn't in a container without crashing).
 */
func TestHttpRequest(t *testing.T) {
	if err := setupDir(); err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(testDir)

	d, err := startDaemon("")
	if err != nil {
		t.Fatal(err)
	}
	defer d.Stop()

	c := http.Client{Transport: &http.Transport{Dial: DevLxdDialer{Path: fmt.Sprintf("%s/devlxd/sock", testDir)}.DevLxdDial}}

	raw, err := c.Get("http://1.0")
	if err != nil {
		t.Fatal(err)
	}

	if raw.StatusCode != 500 {
		t.Fatal(err)
	}

	resp, err := ioutil.ReadAll(raw.Body)
	if err != nil {
		t.Fatal(err)
	}

	if !strings.Contains(string(resp), pidNotInContainerErr.Error()) {
		t.Fatal("resp error not expected: ", string(resp))
	}
}
Beispiel #21
0
func asyncHttpGets(urls []string) []*HttpResponse {
	ch := make(chan *HttpResponse)
	responses := []*HttpResponse{}
	client := http.Client{}
	for _, url := range urls {
		go func(url string) {
			fmt.Printf("Fetching %s \n", url)
			resp, err := client.Get(url)
			ch <- &HttpResponse{url, resp, err}
			if err != nil && resp != nil && resp.StatusCode == http.StatusOK {
				resp.Body.Close()
			}
		}(url)
	}

	for {
		select {
		case r := <-ch:
			fmt.Printf("%s was fetched\n", r.url)
			if r.err != nil {
				fmt.Println("with an error", r.err)
			}
			responses = append(responses, r)
			if len(responses) == len(urls) {
				return responses
			}
		case <-time.After(50 * time.Millisecond):
			fmt.Printf(".")
		}
	}
	return responses
}
Beispiel #22
0
// Check if docker (or dogestry) is running on the endpoint
func ServerCheck(host string, port int, timeout time.Duration, docker bool) bool {
	url := fmt.Sprintf("http://%v:%v/version", host, port)

	if !docker {
		url = fmt.Sprintf("http://%v:%v/status/check", host, port)
	}

	client := http.Client{
		Timeout: timeout,
	}

	resp, getErr := client.Get(url)
	if getErr != nil {
		return false
	}
	defer resp.Body.Close()

	if docker {
		if resp.StatusCode != 200 {
			return false
		}
	} else {
		// Dogestry check
		body, readErr := ioutil.ReadAll(resp.Body)
		if readErr != nil {
			return false
		}

		if string(body) != "OK" {
			return false
		}
	}

	return true
}
// get performs an HTTPS GET to the specified path for a specific node.
func get(t *testing.T, client *http.Client, node *localcluster.Container, path string) []byte {
	url := fmt.Sprintf("https://%s%s", node.Addr(""), path)
	// TODO(bram) #2059: Remove retry logic.
	for r := retry.Start(retryOptions); r.Next(); {
		resp, err := client.Get(url)
		if err != nil {
			t.Logf("could not GET %s - %s", url, err)
			continue
		}
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Logf("could not read body for %s - %s", url, err)
			continue
		}
		if resp.StatusCode != http.StatusOK {
			t.Logf("could not GET %s - statuscode: %d - body: %s", url, resp.StatusCode, body)
			continue
		}
		t.Logf("OK response from %s", url)
		return body
	}
	t.Fatalf("There was an error retrieving %s", url)
	return []byte("")
}
Beispiel #24
0
// GetMetaData retrieves instance metadata about the current machine.
//
// See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html for more details.
func GetMetaData(path string) (contents []byte, err error) {
	c := http.Client{
		Transport: &http.Transport{
			Dial: func(netw, addr string) (net.Conn, error) {
				deadline := time.Now().Add(5 * time.Second)
				c, err := net.DialTimeout(netw, addr, time.Second*2)
				if err != nil {
					return nil, err
				}
				c.SetDeadline(deadline)
				return c, nil
			},
		},
	}

	url := "http://169.254.169.254/latest/meta-data/" + path

	resp, err := c.Get(url)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		err = fmt.Errorf("Code %d returned for url %s", resp.StatusCode, url)
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	return []byte(body), err
}
Beispiel #25
0
/* ============================================================================================ */
func FileGetContents(filename string) string {
	var file []byte
	if strings.Index(filename, "http://") == -1 {
		file, err = ioutil.ReadFile(filename)
		setErr(err)
	} else {
		var timeout = time.Duration(10 * time.Second)
		var dialTimeout = func(network, addr string) (net.Conn, error) {
			return net.DialTimeout(network, addr, timeout)
		}
		transport := http.Transport{
			Dial: dialTimeout,
		}
		client := http.Client{
			Transport: &transport,
		}
		var res *http.Response
		res, err := client.Get(filename)
		setErr(err)
		defer func() {
			if res != nil && res.Body != nil {
				res.Body.Close()
			}
		}()
		if res != nil && res.Body != nil {
			file, err = ioutil.ReadAll(res.Body)
			setErr(err)
		}
	}
	return string(file)
}
Beispiel #26
0
// GetMetadata returns the value for the given meta data item.
func GetMetadata(item Item) (string, error) {
	client := http.Client{
		Transport: &http.Transport{
			// timeout only for dialing, because if we are not in ec2, this will timeout
			Dial: func(network, addr string) (net.Conn, error) {
				return net.DialTimeout(network, addr, DialTimeout)
			},
		},
	}

	resp, err := client.Get(metaDataEndpoint + item.String())
	if err != nil {
		return "", wrapEC2Error(err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return "", fmt.Errorf("err: status code (%d)", resp.StatusCode)
	}

	out, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	return string(out), nil
}
// initRobotAccount creates a GCP robot account for this connector.
func initRobotAccount(userClient *http.Client) (string, string) {
	params := url.Values{}
	params.Set("oauth_client_id", flagToString(gcpOAuthClientIDFlag, lib.DefaultConfig.GCPOAuthClientID))

	url := fmt.Sprintf("%s%s?%s", flagToString(gcpBaseURLFlag, lib.DefaultConfig.GCPBaseURL), "createrobot", params.Encode())
	response, err := userClient.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	if response.StatusCode != 200 {
		log.Fatal("failed to initialize robot account: " + response.Status)
	}

	var robotInit struct {
		Success  bool   `json:"success"`
		Message  string `json:"message"`
		XMPPJID  string `json:"xmpp_jid"`
		AuthCode string `json:"authorization_code"`
	}

	if err = json.NewDecoder(response.Body).Decode(&robotInit); err != nil {
		log.Fatal(err)
	}
	if !robotInit.Success {
		log.Fatal("failed to initialize robot account: " + robotInit.Message)
	}

	fmt.Println("Requested OAuth credentials for robot account")

	return robotInit.XMPPJID, robotInit.AuthCode
}
Beispiel #28
0
func urlToNode(url string, client *http.Client, redirect int) (*html.Node, error) {
	if redirect >= MAX_REDIRECT {
		return nil, fmt.Errorf("Too many canonical redirection at %s", url)
	}
	resp, err := client.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		if resp.StatusCode < 500 {
			return nil, nil
		} else {
			return nil, fmt.Errorf("Server returns %d code (url: %s).", resp.StatusCode, url)
		}
	}

	root, err := html.Parse(resp.Body)
	if err != nil {
		return nil, err
	}
	s := getCanonicalUrl(root)
	if s != "" && s != url {
		return urlToNode(s, client, redirect+1)
	}
	return root, err
}
Beispiel #29
0
// https://github.com/coreos/clair/blob/master/docs/API.md#get-a-layers-vulnerabilities
func (clair ClairAPI) GetLayerVuln(templateName string) (vulnList []VulnerabilityItem, err error) {
	var client *http.Client
	url := clair.Address + ":" + strconv.Itoa(clair.Port) + "/v1/layers/" + templateName + "/vulnerabilities" + "?minimumPriority=" + clair.MinimumPriority
	if clair.HttpsEnable {
		url = "https://" + url
		client = tlsClient
	} else {
		url = "http://" + url
		client = httpClient
	}

	response, err := client.Get(url)
	if err != nil {
		log.Println("Send request failed request: ", err)
		return vulnList, err
	}
	defer response.Body.Close()
	body, _ := ioutil.ReadAll(response.Body)

	// if OK  - returned "200 OK"
	if response.StatusCode != 200 {
		log.Println("Error - response not ok - ", response.Status, " with message: ", string(body))
		return vulnList, errors.New(string(body))
	}

	var result GetLayersVulnResponseAPI
	err = json.Unmarshal(body, &result)
	if err != nil {
		log.Println("Cannot parse answer from clair to json: ", err)
		return vulnList, err
	}
	vulnList = result.Vulnerabilities
	return vulnList, nil
}
func responseCert(client *http.Client, res *http.Response, bundle bool) ([][]byte, error) {
	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, fmt.Errorf("acme: response stream: %v", err)
	}
	cert := [][]byte{b}
	if !bundle {
		return cert, nil
	}

	// append ca cert
	up := linkHeader(res.Header, "up")
	if up == "" {
		return nil, errors.New("acme: rel=up link not found")
	}
	res, err = client.Get(up)
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()
	if res.StatusCode != http.StatusOK {
		return nil, responseError(res)
	}
	b, err = ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}
	return append(cert, b), nil
}