Skip to content

Commit

Permalink
fix: delete lsconfig content if invalid json
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ committed Nov 5, 2024
1 parent 627b730 commit a8178e7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 12 deletions.
8 changes: 4 additions & 4 deletions .github/upload-to-s3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function uploadFile() {
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
copyOrDownloadToTemp $FILENAME_SRC "$FILENAME_DST" $DRY_RUN

FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_darwin_arm64/snyk-ls"
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_darwin_arm64_v8.0/snyk-ls"
FILENAME_DST="snyk-ls_${VERSION}_darwin_arm64"
# shellcheck disable=SC2086
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
Expand All @@ -80,19 +80,19 @@ function uploadFile() {
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
copyOrDownloadToTemp $FILENAME_SRC "$FILENAME_DST" $DRY_RUN

FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_linux_386/snyk-ls"
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_linux_386_sse2/snyk-ls"
FILENAME_DST="snyk-ls_${VERSION}_linux_386"
# shellcheck disable=SC2086
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
copyOrDownloadToTemp $FILENAME_SRC "$FILENAME_DST" $DRY_RUN

FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_linux_arm64/snyk-ls"
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_linux_arm64_v8.0/snyk-ls"
FILENAME_DST="snyk-ls_${VERSION}_linux_arm64"
# shellcheck disable=SC2086
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
copyOrDownloadToTemp $FILENAME_SRC "$FILENAME_DST" $DRY_RUN

FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_windows_386/snyk-ls.exe"
FILENAME_SRC="$SCRIPT_DIR/../build/snyk-ls_windows_386_sse2/snyk-ls.exe"
FILENAME_DST="snyk-ls_${VERSION}_windows_386.exe"
# shellcheck disable=SC2086
uploadFile $FILENAME_SRC $FILENAME_DST $DRY_RUN
Expand Down
34 changes: 28 additions & 6 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,28 @@ on:
pull_request:

jobs:
lint:
name: lint
runs-on: ubuntu-latest
steps:
- name: Prepare git
run: git config --global core.autocrlf false

- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: "./go.mod"
cache: "true"

- name: Lint source code
run: |
make tools lint
unit-tests:
name: unit tests
needs: [lint]
runs-on: ubuntu-latest
steps:
- name: Prepare git
Expand Down Expand Up @@ -49,10 +69,6 @@ jobs:
run: |
make tools
- name: Lint source code
run: |
make tools lint
- name: Run tests
env:
DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}}
Expand All @@ -70,6 +86,7 @@ jobs:
integration-tests:
name: integration-tests
needs: [lint]
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand Down Expand Up @@ -99,12 +116,13 @@ jobs:
run: |
make tools
- name: Run integration tests with Pact
- name: Run integration & smoke tests with Pact
if: matrix.os == 'ubuntu-latest'
env:
DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}}
SNYK_TOKEN: ${{secrets.SNYK_TOKEN }}
INTEG_TESTS: "true"
SMOKE_TESTS: "true"
run: |
export PATH=$PATH:~/pact/bin
Expand All @@ -121,6 +139,7 @@ jobs:
DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}}
SNYK_TOKEN: ${{secrets.SNYK_TOKEN }}
INTEG_TESTS: "true"
SMOKE_TESTS: "true"
run: |
export PATH=$PATH:~/pact/bin
Expand All @@ -136,12 +155,14 @@ jobs:
DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}}
SNYK_TOKEN: ${{secrets.SNYK_TOKEN }}
INTEG_TESTS: "true"
SMOKE_TESTS: "true"
run: |
make clean test
proxy-test:
name: proxy-test
needs: [lint]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -154,6 +175,7 @@ jobs:
race-tests:
name: race-test
needs: [lint]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -216,6 +238,7 @@ jobs:
push: true
test-release:
name: test-release
needs: [lint, unit-tests]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -259,4 +282,3 @@ jobs:
AWS_S3_BUCKET_NAME: ${{ secrets.AWS_S3_BUCKET_NAME }}
run: |
.github/upload-to-s3.sh --dryrun
6 changes: 6 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,30 @@ jobs:
chmod +x build/snyk-ls_linux_amd64_v1/snyk-ls
build/snyk-ls_linux_amd64_v1/snyk-ls -licenses
# we only want to upload when we consciously release, not on merge
- name: Login to AWS
if: github.event_name == 'workflow_dispatch'
run: |
.github/setup_aws_credentials.py \
--role-arn "arn:aws:iam::198361731867:role/Snyk-Assets-WriteOnly" \
--region "${{ secrets.AWS_REGION }}"
- name: Upload binaries to static.snyk.io
if: github.event_name == 'workflow_dispatch'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
AWS_S3_BUCKET_NAME: ${{ secrets.AWS_S3_BUCKET_NAME }}
run: |
.github/upload-to-s3.sh
# creating PR in cli repository needs ssh agent
- uses: webfactory/[email protected]
if: github.event_name != 'workflow_dispatch'
with:
ssh-private-key: ${{ secrets.TEAM_IDE_USER_SSH }}

- name: Create PR in CLI to integrate LS
if: github.event_name != 'workflow_dispatch'
env:
GH_TOKEN: ${{ secrets.HAMMERHEAD_GITHUB_PAT_SNYKLS }}
GITHUB_TOKEN: ${{ secrets.HAMMERHEAD_GITHUB_PAT_SNYKLS }}
Expand Down
48 changes: 46 additions & 2 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ package storage

import (
"context"
"encoding/json"
"errors"
"os"
"sync"
"time"

"github.com/adrg/xdg"
Expand All @@ -36,33 +40,72 @@ type StorageWithCallbacks interface {
type storage struct {
callbacks map[string]StorageCallbackFunc
jsonStorage *configuration.JsonStorage
storageFile string
mutex sync.RWMutex
}

func (s *storage) Refresh(config configuration.Configuration, key string) error {
return s.jsonStorage.Refresh(config, key)
s.mutex.Lock()

contents, err := os.ReadFile(s.storageFile)
if err != nil {
s.mutex.Unlock()
return err
}
doc := map[string]interface{}{}
err = json.Unmarshal(contents, &doc)
if err != nil {
s.mutex.Unlock()
return err
}

s.mutex.Unlock()
if value, ok := doc[key]; ok {
config.Set(key, value)
}
return nil
}

func (s *storage) Lock(ctx context.Context, retryDelay time.Duration) error {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.jsonStorage.Lock(ctx, retryDelay)
}

func (s *storage) Unlock() error {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.jsonStorage.Unlock()
}

type storageOption func(*storage)

func (s *storage) Set(key string, value any) error {
s.mutex.Lock()
defer s.mutex.Unlock()

callback := s.callbacks[key]

if callback != nil {
callback(key, value)
}

err := s.jsonStorage.Set(key, value)
if err != nil {

var syntaxError *json.SyntaxError
if errors.As(err, &syntaxError) {
err = os.WriteFile(s.storageFile, []byte("{}"), 0666)
if err != nil {
return err
}
err = s.jsonStorage.Set(key, value)
if err != nil {
return err
}
} else if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -101,5 +144,6 @@ func WithCallbacks(callbacks map[string]StorageCallbackFunc) func(*storage) {
func WithStorageFile(file string) func(*storage) {
return func(s *storage) {
s.jsonStorage = configuration.NewJsonStorage(file)
s.storageFile = file
}
}
24 changes: 24 additions & 0 deletions internal/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ func Test_StorageCallsRegisterCallbacksForKeys(t *testing.T) {
}, 5*time.Second, time.Millisecond, "callback was not called")
}

func Test_StorageCallsRegisterCallbacks_InvalidJsonContent_ShouldClean(t *testing.T) {
called := make(chan bool, 1)
callbacks := map[string]StorageCallbackFunc{}
file := filepath.Join(t.TempDir(), t.Name())
err := os.WriteFile(file, []byte("{\"INTERNAL_OAUTH_TOKEN_STORAGE\":\"{\\\"access_token\\\":\\\"mytoken\\\",\\\"token_type\\\":\\\"bearer\\\",\\\"refresh_token\\\":\\\"myrefreshtoken\\\",\\\"expiry\\\":\\\"2024-10-01T21:43:26.209852+02:00\\\"}\"}\"snyk_token\":\"\"}"), 0644)
assert.NoError(t, err)
myCallback := func(_ string, _ any) { called <- true }

key := "test"
value := "test"
callbacks[key] = myCallback
s, err := NewStorageWithCallbacks(WithCallbacks(callbacks), WithStorageFile(file))
require.NoError(t, err)

err = s.Set(key, value)
require.NoError(t, err)
content, err := os.ReadFile(file)
assert.NoError(t, err)
assert.Equal(t, "{\"test\":\"test\"}", string(content))
require.Eventuallyf(t, func() bool {
return <-called
}, 5*time.Second, time.Millisecond, "callback was not called")
}

func Test_ParallelFileLocking(t *testing.T) {
t.Run("should respect locking order", func(t *testing.T) {
file := filepath.Join(t.TempDir(), t.Name())
Expand Down

0 comments on commit a8178e7

Please sign in to comment.