func()

in internal/beater/api/config/agent/handler.go [76:129]


func (h *handler) Handle(c *request.Context) {
	// error handling
	c.ResponseWriter.Header().Set(headers.CacheControl, errCacheControl)

	query, queryErr := buildQuery(c)
	if queryErr != nil {
		extractQueryError(c, queryErr)
		c.WriteResult()
		return
	}
	if query.Service.Environment == "" {
		query.Service.Environment = h.defaultServiceEnvironment
	}

	// Only service, and not agent, is known for config queries.
	// For anonymous/untrusted agents, we filter the results using
	// query.InsecureAgents below.
	authResource := auth.Resource{ServiceName: query.Service.Name}
	if err := auth.Authorize(c.Request.Context(), auth.ActionAgentConfig, authResource); err != nil {
		if errors.Is(err, auth.ErrUnauthorized) {
			id := request.IDResponseErrorsForbidden
			status := request.MapResultIDToStatus[id]
			c.Result.Set(id, status.Code, err.Error(), nil, nil)
		} else {
			c.Result.SetDefault(request.IDResponseErrorsServiceUnavailable)
			c.Result.Err = err
		}
		c.WriteResult()
		return
	}
	if c.Authentication.Method == auth.MethodAnonymous {
		// Unauthenticated client, restrict results.
		query.InsecureAgents = h.allowAnonymousAgents
	}

	result, err := h.f.Fetch(c.Request.Context(), query)
	if err != nil {
		extractInternalError(c, err)
		c.WriteResult()
		return
	}

	// configuration successfully fetched
	c.ResponseWriter.Header().Set(headers.CacheControl, h.cacheControl)
	c.ResponseWriter.Header().Set(headers.Etag, fmt.Sprintf("\"%s\"", result.Source.Etag))
	c.ResponseWriter.Header().Set(headers.AccessControlExposeHeaders, headers.Etag)

	if result.Source.Etag == ifNoneMatch(c) {
		c.Result.SetDefault(request.IDResponseValidNotModified)
	} else {
		c.Result.SetWithBody(request.IDResponseValidOK, result.Source.Settings)
	}
	c.WriteResult()
}