• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / deployments / 913012443

pending completion
913012443

Pull #869

gitlab-ci

alfrunes
ci: Bump golangci-lint to v1.15.3 (go1.20)

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #869: feat: New configuration parameter storage.proxy_uri

179 of 253 new or added lines in 6 files covered. (70.75%)

2 existing lines in 2 files now uncovered.

7373 of 9353 relevant lines covered (78.83%)

33.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.87
/server.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package main
16

17
import (
18
        "context"
19
        "encoding/base64"
20
        "net/http"
21
        "net/url"
22
        "strings"
23
        "time"
24

25
        "github.com/ant0ine/go-json-rest/rest"
26
        "github.com/pkg/errors"
27

28
        "github.com/mendersoftware/go-lib-micro/config"
29

30
        api "github.com/mendersoftware/deployments/api/http"
31
        "github.com/mendersoftware/deployments/app"
32
        "github.com/mendersoftware/deployments/client/reporting"
33
        dconfig "github.com/mendersoftware/deployments/config"
34
        "github.com/mendersoftware/deployments/storage"
35
        "github.com/mendersoftware/deployments/storage/azblob"
36
        "github.com/mendersoftware/deployments/storage/manager"
37
        "github.com/mendersoftware/deployments/storage/s3"
38
        mstore "github.com/mendersoftware/deployments/store/mongo"
39
)
40

41
func SetupS3(ctx context.Context, defaultOptions *s3.Options) (storage.ObjectStorage, error) {
1✔
42
        c := config.Config
1✔
43

1✔
44
        bucket := c.GetString(dconfig.SettingStorageBucket)
1✔
45

1✔
46
        // Copy / merge defaultOptions
1✔
47
        options := s3.NewOptions(defaultOptions).
1✔
48
                SetBucketName(bucket).
1✔
49
                SetForcePathStyle(c.GetBool(dconfig.SettingAwsS3ForcePathStyle)).
1✔
50
                SetUseAccelerate(c.GetBool(dconfig.SettingAwsS3UseAccelerate))
1✔
51

1✔
52
        // The following parameters falls back on AWS_* environment if not set
1✔
53
        if c.IsSet(dconfig.SettingAwsS3Region) {
2✔
54
                options.SetRegion(c.GetString(dconfig.SettingAwsS3Region))
1✔
55
        }
1✔
56
        if c.IsSet(dconfig.SettingsAwsAuth) ||
1✔
57
                (c.IsSet(dconfig.SettingAwsAuthKeyId) &&
1✔
58
                        c.IsSet(dconfig.SettingAwsAuthSecret)) {
2✔
59
                options.SetStaticCredentials(
1✔
60
                        c.GetString(dconfig.SettingAwsAuthKeyId),
1✔
61
                        c.GetString(dconfig.SettingAwsAuthSecret),
1✔
62
                        c.GetString(dconfig.SettingAwsAuthToken),
1✔
63
                )
1✔
64
        }
1✔
65
        if c.IsSet(dconfig.SettingAwsURI) {
2✔
66
                options.SetURI(c.GetString(dconfig.SettingAwsURI))
1✔
67
        }
1✔
68
        if c.IsSet(dconfig.SettingAwsExternalURI) {
2✔
69
                options.SetExternalURI(c.GetString(dconfig.SettingAwsExternalURI))
1✔
70
        }
1✔
71
        if c.IsSet(dconfig.SettingStorageProxyURI) {
1✔
NEW
72
                rawURL := c.GetString(dconfig.SettingStorageProxyURI)
×
NEW
73
                proxyURL, err := url.Parse(rawURL)
×
NEW
74
                if err != nil {
×
NEW
75
                        return nil, errors.WithMessage(err, "invalid setting `storage.proxy_uri`")
×
NEW
76
                }
×
NEW
77
                options.SetProxyURI(proxyURL)
×
78
        }
79
        if c.IsSet(dconfig.SettingAwsUnsignedHeaders) {
2✔
80
                options.SetUnsignedHeaders(c.GetStringSlice(dconfig.SettingAwsUnsignedHeaders))
1✔
81
        }
1✔
82

83
        storage, err := s3.New(ctx, options)
1✔
84
        return storage, err
1✔
85
}
86

87
func SetupBlobStorage(
88
        ctx context.Context,
89
        defaultOptions *azblob.Options,
90
) (storage.ObjectStorage, error) {
×
91
        c := config.Config
×
92

×
93
        // Copy / merge options
×
94
        options := azblob.NewOptions(defaultOptions)
×
95

×
96
        if c.IsSet(dconfig.SettingAzureConnectionString) {
×
97
                options.SetConnectionString(c.GetString(dconfig.SettingAzureConnectionString))
×
98
        } else if c.IsSet(dconfig.SettingAzureSharedKeyAccount) &&
×
99
                c.IsSet(dconfig.SettingAzureSharedKeyAccountKey) {
×
100
                creds := azblob.SharedKeyCredentials{
×
101
                        AccountName: c.GetString(dconfig.SettingAzureSharedKeyAccount),
×
102
                        AccountKey:  c.GetString(dconfig.SettingAzureSharedKeyAccountKey),
×
103
                }
×
104
                if c.IsSet(dconfig.SettingAzureSharedKeyURI) {
×
105
                        uri := c.GetString(dconfig.SettingAzureSharedKeyURI)
×
106
                        creds.URI = &uri
×
107
                }
×
108
                options.SetSharedKey(creds)
×
109
        }
NEW
110
        if c.IsSet(dconfig.SettingStorageProxyURI) {
×
NEW
111
                rawURL := c.GetString(dconfig.SettingStorageProxyURI)
×
NEW
112
                proxyURL, err := url.Parse(rawURL)
×
NEW
113
                if err != nil {
×
NEW
114
                        return nil, errors.WithMessage(err, "invalid setting `storage.proxy_uri`")
×
NEW
115
                }
×
NEW
116
                options.SetProxyURI(proxyURL)
×
117
        }
UNCOV
118
        return azblob.New(ctx, c.GetString(dconfig.SettingStorageBucket), options)
×
119
}
120

121
func SetupObjectStorage(ctx context.Context) (objManager storage.ObjectStorage, err error) {
1✔
122
        c := config.Config
1✔
123

1✔
124
        // Calculate s3 multipart buffer size: the minimum buffer size that
1✔
125
        // covers the maximum image size aligned to multiple of 5MiB.
1✔
126
        maxImageSize := c.GetInt64(dconfig.SettingStorageMaxImageSize)
1✔
127
        bufferSize := (((maxImageSize - 1) /
1✔
128
                (s3.MultipartMaxParts * s3.MultipartMinSize)) + 1) *
1✔
129
                s3.MultipartMinSize
1✔
130
        var (
1✔
131
                s3Options = s3.NewOptions().
1✔
132
                                SetContentType(app.ArtifactContentType).
1✔
133
                                SetBufferSize(int(bufferSize))
1✔
134
                azOptions = azblob.NewOptions().
1✔
135
                                SetContentType(app.ArtifactContentType)
1✔
136
        )
1✔
137
        var defaultStorage storage.ObjectStorage
1✔
138
        switch defType := c.GetString(dconfig.SettingDefaultStorage); defType {
1✔
139
        case dconfig.StorageTypeAWS:
1✔
140
                defaultStorage, err = SetupS3(ctx, s3Options)
1✔
141
        case dconfig.StorageTypeAzure:
×
142
                defaultStorage, err = SetupBlobStorage(ctx, azOptions)
×
143
        default:
×
144
                err = errors.Errorf(
×
145
                        `storage type must be one of %q or %q, received value %q`,
×
146
                        dconfig.StorageTypeAWS, dconfig.StorageTypeAzure, defType,
×
147
                )
×
148
        }
149
        if err != nil {
1✔
150
                return nil, err
×
151
        }
×
152
        return manager.New(ctx, defaultStorage, s3Options, azOptions)
1✔
153
}
154

155
func RunServer(ctx context.Context) error {
1✔
156
        c := config.Config
1✔
157
        dbClient, err := mstore.NewMongoClient(ctx, c)
1✔
158
        if err != nil {
1✔
159
                return err
×
160
        }
×
161
        defer func() {
1✔
162
                _ = dbClient.Disconnect(context.Background())
×
163
        }()
×
164

165
        ds := mstore.NewDataStoreMongoWithClient(dbClient)
1✔
166

1✔
167
        // Storage Layer
1✔
168
        objStore, err := SetupObjectStorage(ctx)
1✔
169
        if err != nil {
1✔
170
                return errors.WithMessage(err, "main: failed to setup storage client")
×
171
        }
×
172

173
        app := app.NewDeployments(ds, objStore)
1✔
174
        if addr := c.GetString(dconfig.SettingReportingAddr); addr != "" {
2✔
175
                c := reporting.NewClient(addr)
1✔
176
                app = app.WithReporting(c)
1✔
177
        }
1✔
178

179
        // Setup API Router configuration
180
        base64Repl := strings.NewReplacer("-", "+", "_", "/", "=", "")
1✔
181
        expireSec := c.GetDuration(dconfig.SettingPresignExpireSeconds)
1✔
182
        apiConf := api.NewConfig().
1✔
183
                SetPresignExpire(time.Second * expireSec).
1✔
184
                SetPresignHostname(c.GetString(dconfig.SettingPresignHost)).
1✔
185
                SetPresignScheme(c.GetString(dconfig.SettingPresignScheme)).
1✔
186
                SetMaxImageSize(c.GetInt64(dconfig.SettingStorageMaxImageSize)).
1✔
187
                SetEnableDirectUpload(c.GetBool(dconfig.SettingStorageEnableDirectUpload)).
1✔
188
                SetEnableDirectUploadSkipVerify(c.GetBool(dconfig.SettingStorageDirectUploadSkipVerify))
1✔
189
        if key, err := base64.RawStdEncoding.DecodeString(
1✔
190
                base64Repl.Replace(
1✔
191
                        c.GetString(dconfig.SettingPresignSecret),
1✔
192
                ),
1✔
193
        ); err == nil {
2✔
194
                apiConf.SetPresignSecret(key)
1✔
195
        }
1✔
196
        router, err := api.NewRouter(ctx, app, ds, apiConf)
1✔
197
        if err != nil {
1✔
198
                return err
×
199
        }
×
200

201
        api := rest.NewApi()
1✔
202
        SetupMiddleware(c, api)
1✔
203
        api.SetApp(router)
1✔
204

1✔
205
        listen := c.GetString(dconfig.SettingListen)
1✔
206

1✔
207
        if c.IsSet(dconfig.SettingHttps) {
1✔
208

×
209
                cert := c.GetString(dconfig.SettingHttpsCertificate)
×
210
                key := c.GetString(dconfig.SettingHttpsKey)
×
211

×
212
                return http.ListenAndServeTLS(listen, cert, key, api.MakeHandler())
×
213
        }
×
214

215
        return http.ListenAndServe(listen, api.MakeHandler())
1✔
216
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc