Ejemplo n.º 1
0
// Get fetches existing ingresses from the api server
func (listener IngressListener) Get() (Ingresses, error) {
	req, err := http.NewRequest("GET", fmt.Sprintf("https://%s%s", listener.masterIP, listener.getPath), nil)
	if err != nil {
		return Ingresses{}, err
	}

	req.Header.Set("Accept", "application/json")
	req.SetBasicAuth(listener.user, listener.password)

	var data []byte
	work := func() error {
		response, err := listener.httpClient.Do(req)
		if err != nil {
			return err
		}

		if response.StatusCode != 200 {
			return fmt.Errorf("Non-200 response: %d\n", response.StatusCode)
		}

		data, err = ioutil.ReadAll(response.Body)
		return err
	}
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	var ingresses Ingresses
	if err := retry.Try(work); err != nil {
		return ingresses, err
	}

	err = json.Unmarshal(data, &ingresses)
	return ingresses, err
}
Ejemplo n.º 2
0
func (client Client) GetRawFile(repositoryProjectKey, repositorySlug, filePath, branch string) ([]byte, error) {
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	var data []byte
	work := func() error {
		req, err := http.NewRequest("GET", fmt.Sprintf("%s/projects/%s/repos/%s/browse/%s?at=%s&raw", client.baseURL.String(), strings.ToLower(repositoryProjectKey), strings.ToLower(repositorySlug), filePath, branch), nil)
		if err != nil {
			return err
		}
		log.Printf("stash.GetRawFile %s\n", req.URL)
		req.SetBasicAuth(client.userName, client.password)

		var responseCode int
		responseCode, data, err = consumeResponse(req)
		if err != nil {
			return err
		}
		if responseCode != http.StatusOK {
			var reason string = "unhandled reason"
			switch {
			case responseCode == http.StatusNotFound:
				reason = "Not found"
			case responseCode == http.StatusUnauthorized:
				reason = "Unauthorized"
			}
			return errorResponse{StatusCode: responseCode, Reason: reason}
		}
		return nil
	}

	return data, retry.Try(work)
}
Ejemplo n.º 3
0
// GetRepository returns a repository representation for the given Stash Project key and repository slug.
func (client Client) DeleteBranchRestriction(projectKey, repositorySlug string, id int) error {
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	work := func() error {
		req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/rest/branch-permissions/1.0/projects/%s/repos/%s/restricted/%d", client.baseURL.String(), projectKey, repositorySlug, id), nil)
		if err != nil {
			return err
		}
		log.Printf("stash.DeleteBranchRestriction %s\n", req.URL)
		req.Header.Set("Accept", "application/json")
		req.SetBasicAuth(client.userName, client.password)

		responseCode, _, err := consumeResponse(req)
		if err != nil {
			return err
		}

		if responseCode != http.StatusNoContent {
			var reason string = "unhandled reason"
			switch {
			case responseCode == http.StatusNotFound:
				reason = "Not found"
			case responseCode == http.StatusUnauthorized:
				reason = "Unauthorized"
			}
			return errorResponse{StatusCode: responseCode, Reason: reason}
		}

		return nil
	}

	return retry.Try(work)
}
Ejemplo n.º 4
0
// GetTags returns a map of tags indexed by tag display name for the given repository.
func (client Client) GetTags(projectKey, repositorySlug string) (map[string]Tag, error) {
	start := 0
	tags := make(map[string]Tag)
	morePages := true
	for morePages {
		var data []byte
		retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)
		work := func() error {
			req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s/tags?start=%d&limit=%d", client.baseURL.String(), projectKey, repositorySlug, start, stashPageLimit), nil)
			if err != nil {
				return err
			}
			req.Header.Set("Accept", "application/json")

			// use credentials if we have them.  If not, the repository must be public.
			if client.userName != "" && client.password != "" {
				req.SetBasicAuth(client.userName, client.password)
			}

			var responseCode int
			responseCode, data, err = consumeResponse(req)
			if err != nil {
				return err
			}

			if responseCode != http.StatusOK {
				var reason string = "unhandled reason"
				switch {
				case responseCode == http.StatusNotFound:
					reason = "Not found"
				case responseCode == http.StatusUnauthorized:
					reason = "Unauthorized"
				}
				return errorResponse{StatusCode: responseCode, Reason: reason}
			}
			return nil
		}
		if err := retry.Try(work); err != nil {
			return nil, err
		}

		var r Tags
		if err := json.Unmarshal(data, &r); err != nil {
			return nil, err
		}
		for _, tag := range r.Tags {
			tags[tag.DisplayID] = tag
		}
		morePages = !r.IsLastPage
		start = r.NextPageStart
	}
	return tags, nil
}
Ejemplo n.º 5
0
// GetRepositories returns a map of repositories indexed by repository URL.
func (client Client) GetRepositories() (map[int]Repository, error) {
	start := 0
	repositories := make(map[int]Repository)
	morePages := true
	for morePages {
		retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)
		var data []byte
		work := func() error {
			req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/repos?start=%d&limit=%d", client.baseURL.String(), start, stashPageLimit), nil)
			if err != nil {
				return err
			}
			Log.Printf("stash.GetRepositories URL %s\n", req.URL)
			req.Header.Set("Accept", "application/json")
			// use credentials if we have them.  If not, the repository must be public.
			if client.userName != "" && client.password != "" {
				req.SetBasicAuth(client.userName, client.password)
			}

			var responseCode int
			responseCode, data, err = consumeResponse(req)
			if err != nil {
				return err
			}
			if responseCode != http.StatusOK {
				var reason string = "unhandled reason"
				switch {
				case responseCode == http.StatusBadRequest:
					reason = "Bad request."
				}
				return errorResponse{StatusCode: responseCode, Reason: reason}
			}
			return nil
		}
		if err := retry.Try(work); err != nil {
			return nil, err
		}

		var r Repositories
		err := json.Unmarshal(data, &r)
		if err != nil {
			return nil, err
		}
		for _, repo := range r.Repository {
			repositories[repo.ID] = repo
		}
		morePages = !r.IsLastPage
		start = r.NextPageStart
	}
	return repositories, nil
}
Ejemplo n.º 6
0
// GetBranches returns a map of branches indexed by branch display name for the given repository.
func (client Client) GetBranches(projectKey, repositorySlug string) (map[string]Branch, error) {
	start := 0
	branches := make(map[string]Branch)
	morePages := true
	for morePages {
		var data []byte
		retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)
		workit := func() error {
			req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s/branches?start=%d&limit=%d", client.baseURL.String(), projectKey, repositorySlug, start, stashPageLimit), nil)
			if err != nil {
				return err
			}
			req.Header.Set("Accept", "application/json")
			req.SetBasicAuth(client.userName, client.password)

			var responseCode int
			responseCode, data, err = consumeResponse(req)
			if err != nil {
				return err
			}

			if responseCode != http.StatusOK {
				var reason string = "unhandled reason"
				switch {
				case responseCode == http.StatusNotFound:
					reason = "Not found"
				case responseCode == http.StatusUnauthorized:
					reason = "Unauthorized"
				}
				return errorResponse{StatusCode: responseCode, Reason: reason}
			}
			return nil
		}
		if err := retry.Try(workit); err != nil {
			return nil, err
		}

		var r Branches
		if err := json.Unmarshal(data, &r); err != nil {
			return nil, err
		}
		for _, branch := range r.Branch {
			branches[branch.DisplayID] = branch
		}
		morePages = !r.IsLastPage
		start = r.NextPageStart
	}
	return branches, nil
}
Ejemplo n.º 7
0
// GetPullRequests returns a list of pull requests for a project / slug.
func (client Client) GetPullRequests(projectKey, projectSlug, state string) ([]PullRequest, error) {
	start := 0
	pullRequests := make([]PullRequest, 0)
	morePages := true
	for morePages {
		retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)
		var data []byte
		work := func() error {
			req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s/pull-requests?state=%s&start=%d&limit=%d", client.baseURL.String(), projectKey, projectSlug, state, start, stashPageLimit), nil)
			if err != nil {
				return err
			}
			req.Header.Set("Accept", "application/json")
			req.SetBasicAuth(client.userName, client.password)

			var responseCode int
			responseCode, data, err = consumeResponse(req)
			if err != nil {
				return err
			}
			if responseCode != http.StatusOK {
				var reason string = "unhandled reason"
				switch {
				case responseCode == http.StatusBadRequest:
					reason = "Bad request."
				}
				return errorResponse{StatusCode: responseCode, Reason: reason}
			}
			return nil
		}
		if err := retry.Try(work); err != nil {
			return nil, err
		}

		var r PullRequests
		err := json.Unmarshal(data, &r)
		if err != nil {
			return nil, err
		}
		for _, pr := range r.PullRequests {
			pullRequests = append(pullRequests, pr)
		}
		morePages = !r.IsLastPage
		start = r.NextPageStart
	}
	return pullRequests, nil
}
Ejemplo n.º 8
0
// GetRepository returns a repository representation for the given Stash Project key and repository slug.
func (client Client) GetRepository(projectKey, repositorySlug string) (Repository, error) {
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	var r Repository
	work := func() error {
		req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s", client.baseURL.String(), projectKey, repositorySlug), nil)
		if err != nil {
			return err
		}
		Log.Printf("stash.GetRepository %s\n", req.URL)
		req.Header.Set("Accept", "application/json")
		// use credentials if we have them.  If not, the repository must be public.
		if client.userName != "" && client.password != "" {
			req.SetBasicAuth(client.userName, client.password)
		}

		responseCode, data, err := consumeResponse(req)
		if err != nil {
			return err
		}

		if responseCode != http.StatusOK {
			var reason string = "unhandled reason"
			switch {
			case responseCode == http.StatusNotFound:
				reason = "Not found"
			case responseCode == http.StatusUnauthorized:
				reason = "Unauthorized"
			}
			return errorResponse{StatusCode: responseCode, Reason: reason}
		}

		err = json.Unmarshal(data, &r)
		if err != nil {
			return err
		}
		return nil
	}

	return r, retry.Try(work)
}
Ejemplo n.º 9
0
func (client Client) GetBranchRestrictions(projectKey, repositorySlug string) (BranchRestrictions, error) {
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	var branchRestrictions BranchRestrictions
	work := func() error {
		req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/branch-permissions/1.0/projects/%s/repos/%s/restricted", client.baseURL.String(), projectKey, repositorySlug), nil)
		if err != nil {
			return err
		}
		log.Printf("stash.GetBranchRestrictions %s\n", req.URL)
		req.Header.Set("Accept", "application/json")
		req.SetBasicAuth(client.userName, client.password)

		responseCode, data, err := consumeResponse(req)
		if err != nil {
			return err
		}

		if responseCode != http.StatusOK {
			var reason string = "unhandled reason"
			switch {
			case responseCode == http.StatusNotFound:
				reason = "Not found"
			case responseCode == http.StatusUnauthorized:
				reason = "Unauthorized"
			}
			return errorResponse{StatusCode: responseCode, Reason: reason}
		}

		err = json.Unmarshal(data, &branchRestrictions)
		if err != nil {
			return err
		}
		return nil
	}

	return branchRestrictions, retry.Try(work)
}
Ejemplo n.º 10
0
// TokenDetail fetches the details associated with bearer token.
func (client TokenDetailClient) TokenDetail(bearerToken string) (Token, error) {
	var token Token
	work := func() error {
		req, err := http.NewRequest("GET", client.endpointURL, nil)
		req.Header.Add("Authorization", "Bearer: "+bearerToken)

		resp, err := client.httpClient.Do(req)
		if err != nil {
			return err
		}

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

		if err := json.Unmarshal(data, &token); err != nil {
			return err
		}

		return nil
	}

	retry := retry.New(3*time.Second, 3, func(attempts uint) {
		if attempts == 0 {
			return
		}
		time.Sleep((1 << attempts) * time.Second)
	})

	err := retry.Try(work)
	if err != nil {
		return Token{}, err
	}

	return token, nil
}
Ejemplo n.º 11
0
// Listen listens for new ingresses and seeds the cache with the value of init.
func (listener IngressListener) Listen(init Ingresses) {

	go ingressMux(init)

	tlsConfig := &tls.Config{}

	if listener.masterCACertPath == "" {
		tlsConfig.InsecureSkipVerify = true
		Log.Println("Trusting any kube-master certificate")
	} else {
		caCert, err := ioutil.ReadFile(listener.masterCACertPath)
		if err != nil {
			Log.Fatalf("Cannot read CA cert file %s: %v\n", listener.masterCACertPath, err)
		}
		caCertPool := x509.NewCertPool()
		caCertPool.AppendCertsFromPEM(caCert)
		tlsConfig.RootCAs = caCertPool
		Log.Println("kube master secured with TLS")
	}

	websocket.DefaultDialer.TLSClientConfig = tlsConfig
	u := url.URL{Scheme: "wss", Host: listener.masterIP, Path: listener.watchPath}
	wsHeaders := http.Header{
		"Authorization": []string{"Basic " + base64.StdEncoding.EncodeToString([]byte(listener.user+":"+listener.password))},
		"Origin":        []string{"https://" + listener.masterIP + listener.watchPath},
	}

	var c *websocket.Conn
	work := func() error {
		var err error

		c, _, err = websocket.DefaultDialer.Dial(u.String(), wsHeaders)
		if err != nil {
			return err
		}

		return nil
	}

	retry := retry.New(16*time.Second, 5, func(attempts uint) {
		if attempts == 0 {
			return
		}
		if attempts > 2 {
			Log.Printf("Connect to kube master try %d\n", attempts+1)
		}
		time.Sleep((1 << attempts) * time.Second)
	})

	err := retry.Try(work)
	if err != nil {
		Log.Fatalf("Failed to connect to master\n")
	}

	go func() {
		defer c.Close()
		for {
			_, message, err := c.ReadMessage()

			if err != nil {
				Log.Println("read:", err)
			}
			var prettyJSON bytes.Buffer
			if err := json.Indent(&prettyJSON, message, "", "\t"); err != nil {
				Log.Println(err)
				continue
			}
			var ingresses Ingresses
			Log.Printf("\n%s\n", string(prettyJSON.Bytes()))
			if err := json.Unmarshal(message, ingresses); err != nil {
				Log.Println(err)
				continue
			}
			ingressSetChan <- ingresses
		}
	}()
}