Skip to content

Commit

Permalink
Merge branch 'master' into feature/set-retry-bakoff
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrergeru committed Nov 7, 2024
2 parents f9f28f8 + a16c390 commit a6c2738
Show file tree
Hide file tree
Showing 161 changed files with 8,043 additions and 1,750 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '33 20 * * 2'

jobs:
analyze:
Expand All @@ -22,14 +20,14 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: go

- run: make build

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
4 changes: 2 additions & 2 deletions .github/workflows/docker-feature.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
if: ${{github.event.issue.pull_request != null && startsWith(github.event.comment.body, '/build') && github.event.comment.author_association == 'MEMBER'}}
steps:
- name: Get PR branch
uses: xt0rted/pull-request-comment-branch@v1
uses: xt0rted/pull-request-comment-branch@v2
id: comment-branch

- uses: actions/github-script@v6
Expand All @@ -38,7 +38,7 @@ jobs:

- name: Checkout PR branch
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}
ref: ${{ fromJSON(steps.get-pr.outputs.result).head.sha }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ jobs:
name: lint
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version-file: go.mod

- name: Run linter
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v4
with:
version: v1.56.2

2 changes: 1 addition & 1 deletion .github/workflows/publish-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
name: Create Release
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Ruby 3.3
uses: ruby/setup-ruby@v1
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/swagger-delete.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
name: Delete api from SwaggerHub
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '16.16.0'
node-version: '20.17.0'
- run: npm i --location=global swaggerhub-cli
- run: |
VERSION=`echo ${GITHUB_REF_NAME}| sed 's#[^a-zA-Z0-9_\.\-]#_#g'`
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/swagger-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ jobs:
working-directory: .

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- run: make install-swag

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: '16.16.0'
node-version: '20.17.0'
- run: npm install --location=global @openapitools/openapi-generator-cli
- run: make spec
- run: make validate-spec

- name: Save build artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: specfile
path: docs/swagger.yaml
Expand All @@ -45,15 +45,15 @@ jobs:
working-directory: .

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Download spec file artifact
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4.1.7
with:
name: specfile
path: docs

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
- run: npm i --location=global swaggerhub-cli
- run: |
VERSION=`echo ${GITHUB_REF_NAME}| sed 's#[^a-zA-Z0-9_\.\-]#_#g'`
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/swagger-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ jobs:
working-directory: .

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- run: make install-swag

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: '20.11.0'
node-version: '20.17.0'
- run: npm install --location=global @openapitools/openapi-generator-cli
- run: make spec
- run: make validate-spec
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
ports:
- 6379:6379
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 as builder
FROM golang:1.22 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.checker
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 as builder
FROM golang:1.22 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.cli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 as builder
FROM golang:1.22 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.filter
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 as builder
FROM golang:1.22 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.notifier
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 as builder
FROM golang:1.22 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mock:

.PHONY: install-swag
install-swag:
go install github.com/swaggo/swag/cmd/swag@v1.8.12
go install github.com/swaggo/swag/cmd/swag@v1.16.3

.PHONY: spec
spec:
Expand Down
27 changes: 27 additions & 0 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {
MetricsTTL map[moira.ClusterKey]time.Duration
Flags FeatureFlags
Authorization Authorization
Limits LimitsConfig
}

// WebConfig is container for web ui configuration parameters.
Expand All @@ -60,3 +61,29 @@ type MetricSourceCluster struct {
func (WebConfig) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}

const (
// DefaultTriggerNameMaxSize which will be used while validating dto.Trigger.
DefaultTriggerNameMaxSize = 200
)

// LimitsConfig contains limits for some entities.
type LimitsConfig struct {
// Trigger contains limits for triggers.
Trigger TriggerLimits
}

// TriggerLimits contains all limits applied for triggers.
type TriggerLimits struct {
// MaxNameSize is the amount of characters allowed in trigger name.
MaxNameSize int
}

// GetTestLimitsConfig is used for testing.
func GetTestLimitsConfig() LimitsConfig {
return LimitsConfig{
Trigger: TriggerLimits{
MaxNameSize: DefaultTriggerNameMaxSize,
},
}
}
46 changes: 42 additions & 4 deletions api/controller/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"regexp"
"time"

"github.com/go-graphite/carbonapi/date"
Expand Down Expand Up @@ -53,14 +54,11 @@ func GetContactById(database moira.Database, contactID string) (*dto.Contact, *a
func CreateContact(
dataBase moira.Database,
auth *api.Authorization,
contactsTemplate []api.WebContact,
contact *dto.Contact,
userLogin,
teamID string,
) *api.ErrorResponse {
if userLogin != "" && teamID != "" {
return api.ErrorInternalServer(fmt.Errorf("CreateContact: cannot create contact when both userLogin and teamID specified"))
}

if !isAllowedToUseContactType(auth, userLogin, contact.Type) {
return api.ErrorInvalidRequest(ErrNotAllowedContactType)
}
Expand All @@ -78,6 +76,7 @@ func CreateContact(
Type: contact.Type,
Value: contact.Value,
}

if contactData.ID == "" {
uuid4, err := uuid.NewV4()
if err != nil {
Expand All @@ -94,19 +93,26 @@ func CreateContact(
}
}

if err := validateContact(contactsTemplate, contactData); err != nil {
return api.ErrorInvalidRequest(err)
}

if err := dataBase.SaveContact(&contactData); err != nil {
return api.ErrorInternalServer(err)
}

contact.User = contactData.User
contact.ID = contactData.ID
contact.TeamID = contactData.Team

return nil
}

// UpdateContact updates notification contact for current user.
func UpdateContact(
dataBase moira.Database,
auth *api.Authorization,
contactsTemplate []api.WebContact,
contactDTO dto.Contact,
contactData moira.ContactData,
) (dto.Contact, *api.ErrorResponse) {
Expand All @@ -117,12 +123,24 @@ func UpdateContact(
contactData.Type = contactDTO.Type
contactData.Value = contactDTO.Value
contactData.Name = contactDTO.Name

if contactDTO.User != "" || contactDTO.TeamID != "" {
contactData.User = contactDTO.User
contactData.Team = contactDTO.TeamID
}

if err := validateContact(contactsTemplate, contactData); err != nil {
return contactDTO, api.ErrorInvalidRequest(err)
}

if err := dataBase.SaveContact(&contactData); err != nil {
return contactDTO, api.ErrorInternalServer(err)
}

contactDTO.User = contactData.User
contactDTO.TeamID = contactData.Team
contactDTO.ID = contactData.ID

return contactDTO, nil
}

Expand Down Expand Up @@ -221,9 +239,11 @@ func CheckUserPermissionsForContact(
}
return moira.ContactData{}, api.ErrorInternalServer(err)
}

if auth.IsAdmin(userLogin) {
return contactData, nil
}

if contactData.Team != "" {
teamContainsUser, err := dataBase.IsTeamContainUser(contactData.Team, userLogin)
if err != nil {
Expand All @@ -233,9 +253,11 @@ func CheckUserPermissionsForContact(
return contactData, nil
}
}

if contactData.User == userLogin {
return contactData, nil
}

return moira.ContactData{}, api.ErrorForbidden("you are not permitted")
}

Expand All @@ -257,3 +279,19 @@ func isAllowedToUseContactType(auth *api.Authorization, userLogin string, contac

return isAllowedContactType || isAdmin || !isAuthEnabled
}

func validateContact(contactsTemplate []api.WebContact, contact moira.ContactData) error {
var validationPattern string
for _, contactTemplate := range contactsTemplate {
if contactTemplate.ContactType == contact.Type {
validationPattern = contactTemplate.ValidationRegex
break
}
}

if matched, err := regexp.MatchString(validationPattern, contact.Value); !matched || err != nil {
return fmt.Errorf("contact value doesn't match regex: '%s'", validationPattern)
}

return nil
}
10 changes: 6 additions & 4 deletions api/controller/contact_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
"github.com/moira-alert/moira/api/dto"
)

func GetContactEventsByIdWithLimit(database moira.Database, contactID string, from int64, to int64) (*dto.ContactEventItemList, *api.ErrorResponse) {
events, err := database.GetNotificationsByContactIdWithLimit(contactID, from, to)
// GetContactEventsHistoryByID is a controller that fetches events from database by using moira.Database.GetNotificationsHistoryByContactID.
func GetContactEventsHistoryByID(database moira.Database, contactID string, from, to, page, size int64,
) (*dto.ContactEventItemList, *api.ErrorResponse) {
events, err := database.GetNotificationsHistoryByContactID(contactID, from, to, page, size)
if err != nil {
return nil, api.ErrorInternalServer(fmt.Errorf("GetContactEventsByIdWithLimit: can't get notifications for contact with id %v", contactID))
return nil, api.ErrorInternalServer(fmt.Errorf("GetContactEventsHistoryByID: can't get notifications for contact with id %v", contactID))
}

eventsList := dto.ContactEventItemList{
List: make([]dto.ContactEventItem, 0),
List: make([]dto.ContactEventItem, 0, len(events)),
}
for _, i := range events {
contactEventItem := &dto.ContactEventItem{
Expand Down
Loading

0 comments on commit a6c2738

Please sign in to comment.