Beispiel #1
0
func (s *Server) BuildEvents(w http.ResponseWriter, r *http.Request) {
	buildIDStr := r.FormValue(":build_id")

	buildID, err := strconv.Atoi(buildIDStr)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	build, found, err := s.db.GetBuild(buildID)
	if err != nil {
		s.logger.Error("failed-to-get-build", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if !found {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if !auth.IsAuthenticated(r) {
		if build.OneOff() {
			s.rejector.Unauthorized(w, r)
			return
		}

		config, _, err := s.db.GetConfigByBuildID(build.ID)
		if err != nil {
			s.logger.Error("failed-to-get-config", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		public, err := config.JobIsPublic(build.JobName)
		if err != nil {
			s.logger.Error("failed-to-see-job-is-public", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !public {
			s.rejector.Unauthorized(w, r)
			return
		}
	}

	streamDone := make(chan struct{})

	go func() {
		defer close(streamDone)
		s.eventHandlerFactory(s.db, buildID).ServeHTTP(w, r)
	}()

	select {
	case <-streamDone:
	case <-s.drain:
	}
}
Beispiel #2
0
func (s *Server) GetResource(pipelineDB db.PipelineDB) http.Handler {
	logger := s.logger.Session("get-resource")
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		config, _, found, err := pipelineDB.GetConfig()
		if err != nil {
			logger.Error("failed-to-get-config", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			logger.Info("config-not-found")
			w.WriteHeader(http.StatusNotFound)
			return
		}

		resourceName := r.FormValue(":resource_name")

		resourceConfig, resourceFound := config.Resources.Lookup(resourceName)
		if !resourceFound {
			logger.Info("resource-not-in-config")
			w.WriteHeader(http.StatusNotFound)
			return
		}

		dbResource, found, err := pipelineDB.GetResource(resourceName)
		if err != nil {
			logger.Error("failed-to-get-resource", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			logger.Debug("resource-not-found", lager.Data{"resource": resourceName})
			w.WriteHeader(http.StatusNotFound)
			return
		}

		resource := present.Resource(
			resourceConfig,
			config.Groups,
			dbResource,
			auth.IsAuthenticated(r),
		)

		w.WriteHeader(http.StatusOK)

		json.NewEncoder(w).Encode(resource)
	})
}
Beispiel #3
0
func (s *Server) ListResources(pipelineDB db.PipelineDB) http.Handler {
	logger := s.logger.Session("list-resources")
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var resources []atc.Resource

		config, _, found, err := pipelineDB.GetConfig()
		if err != nil {
			logger.Error("failed-to-get-config", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		showCheckErr := auth.IsAuthenticated(r)

		for _, resource := range config.Resources {
			dbResource, found, err := pipelineDB.GetResource(resource.Name)
			if err != nil {
				logger.Error("failed-to-get-resource", err)
				w.WriteHeader(http.StatusInternalServerError)
				return
			}

			if !found {
				logger.Debug("resource-not-found", lager.Data{"resource": resource})
				w.WriteHeader(http.StatusNotFound)
				return
			}

			resources = append(
				resources,
				present.Resource(
					resource,
					config.Groups,
					dbResource,
					showCheckErr,
				),
			)
		}

		w.WriteHeader(http.StatusOK)

		json.NewEncoder(w).Encode(resources)
	})
}
Beispiel #4
0
func (server *server) GetResource(pipelineDB db.PipelineDB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		session := server.logger.Session("get-resource")

		resourceName := r.FormValue(":resource")

		startingID, parseErr := strconv.Atoi(r.FormValue("id"))
		if parseErr != nil {
			session.Info("cannot-parse-id-to-int", lager.Data{"id": r.FormValue("id")})
			startingID = 0
		}

		resultsGreaterThanStartingID, parseErr := strconv.ParseBool(r.FormValue("newer"))
		if parseErr != nil {
			resultsGreaterThanStartingID = false
			session.Info("cannot-parse-newer-to-bool", lager.Data{"newer": r.FormValue("newer")})
		}

		authenticated := auth.IsAuthenticated(r)

		templateData, err := FetchTemplateData(pipelineDB, authenticated, resourceName, startingID, resultsGreaterThanStartingID)

		switch err {
		case ErrResourceConfigNotFound:
			session.Error("could-not-find-resource-in-config", ErrResourceConfigNotFound, lager.Data{
				"resource": resourceName,
			})
			w.WriteHeader(http.StatusNotFound)
			return
		case nil:
			break
		default:
			session.Error("failed-to-build-template-data", err, lager.Data{
				"resource": resourceName,
			})
			http.Error(w, "failed to fetch resources", http.StatusInternalServerError)
			return
		}

		err = server.template.Execute(w, templateData)
		if err != nil {
			session.Fatal("failed-to-build-template", err, lager.Data{
				"template-data": templateData,
			})
		}
	})
}
Beispiel #5
0
	BeforeEach(func() {
		fakeValidator = new(fakes.FakeValidator)
		fakeUserContextReader = new(fakes.FakeUserContextReader)

		a := make(chan bool, 1)
		tn := make(chan string, 1)
		ti := make(chan int, 1)
		ia := make(chan bool, 1)
		f := make(chan bool, 1)
		authenticated = a
		teamNameChan = tn
		teamIDChan = ti
		isAdminChan = ia
		foundChan = f
		simpleHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			a <- auth.IsAuthenticated(r)
			teamName, teamID, isAdmin, found := auth.GetTeam(r)
			f <- found
			tn <- teamName
			ti <- teamID
			ia <- isAdmin
		})

		server = httptest.NewServer(auth.WrapHandler(
			simpleHandler,
			fakeValidator,
			fakeUserContextReader,
		))

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