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

mendersoftware / deployments / 1062206731

06 Nov 2023 10:31AM UTC coverage: 77.615% (-3.3%) from 80.909%
1062206731

push

gitlab-ci

web-flow
Merge pull request #948 from alfrunes/upgrade-deps

Upgrade go mod dependencies

13 of 25 new or added lines in 4 files covered. (52.0%)

5 existing lines in 2 files now uncovered.

4133 of 5325 relevant lines covered (77.62%)

63.14 hits per line

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

59.88
/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
        "github.com/mendersoftware/go-lib-micro/log"
30

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

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

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

1✔
47
        // Copy / merge defaultOptions
1✔
48
        options := s3.NewOptions(defaultOptions).
1✔
49
                SetBucketName(bucket).
1✔
50
                SetForcePathStyle(c.GetBool(dconfig.SettingAwsS3ForcePathStyle))
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
        useAccelerate := c.GetBool(dconfig.SettingAwsS3UseAccelerate)
1✔
66
        if c.IsSet(dconfig.SettingAwsURI) {
2✔
67
                options.SetURI(c.GetString(dconfig.SettingAwsURI))
1✔
68
                if useAccelerate {
1✔
NEW
69
                        log.FromContext(ctx).
×
NEW
70
                                Warn(`cannot use s3 transfer acceleration with custom "uri": ` +
×
NEW
71
                                        "disabling transfer acceleration")
×
NEW
72
                }
×
NEW
73
        } else {
×
NEW
74
                options.SetUseAccelerate(c.GetBool(dconfig.SettingAwsS3UseAccelerate))
×
UNCOV
75
        }
×
76
        if c.IsSet(dconfig.SettingAwsExternalURI) {
2✔
77
                options.SetExternalURI(c.GetString(dconfig.SettingAwsExternalURI))
1✔
78
        }
1✔
79
        if c.IsSet(dconfig.SettingStorageProxyURI) {
1✔
80
                rawURL := c.GetString(dconfig.SettingStorageProxyURI)
×
81
                proxyURL, err := url.Parse(rawURL)
×
82
                if err != nil {
×
83
                        return nil, errors.WithMessage(err, "invalid setting `storage.proxy_uri`")
×
84
                }
×
85
                options.SetProxyURI(proxyURL)
×
86
        }
87
        if c.IsSet(dconfig.SettingAwsUnsignedHeaders) {
2✔
88
                options.SetUnsignedHeaders(c.GetStringSlice(dconfig.SettingAwsUnsignedHeaders))
1✔
89
        }
1✔
90

91
        storage, err := s3.New(ctx, options)
1✔
92
        return storage, err
1✔
93
}
94

95
func SetupBlobStorage(
96
        ctx context.Context,
97
        defaultOptions *azblob.Options,
98
) (storage.ObjectStorage, error) {
×
99
        c := config.Config
×
100

×
101
        // Copy / merge options
×
102
        options := azblob.NewOptions(defaultOptions)
×
103

×
104
        if c.IsSet(dconfig.SettingAzureConnectionString) {
×
105
                options.SetConnectionString(c.GetString(dconfig.SettingAzureConnectionString))
×
106
        } else if c.IsSet(dconfig.SettingAzureSharedKeyAccount) &&
×
107
                c.IsSet(dconfig.SettingAzureSharedKeyAccountKey) {
×
108
                creds := azblob.SharedKeyCredentials{
×
109
                        AccountName: c.GetString(dconfig.SettingAzureSharedKeyAccount),
×
110
                        AccountKey:  c.GetString(dconfig.SettingAzureSharedKeyAccountKey),
×
111
                }
×
112
                if c.IsSet(dconfig.SettingAzureSharedKeyURI) {
×
113
                        uri := c.GetString(dconfig.SettingAzureSharedKeyURI)
×
114
                        creds.URI = &uri
×
115
                }
×
116
                options.SetSharedKey(creds)
×
117
        }
118
        if c.IsSet(dconfig.SettingStorageProxyURI) {
×
119
                rawURL := c.GetString(dconfig.SettingStorageProxyURI)
×
120
                proxyURL, err := url.Parse(rawURL)
×
121
                if err != nil {
×
122
                        return nil, errors.WithMessage(err, `invalid setting "storage.proxy_uri"`)
×
123
                }
×
124
                if !strings.HasPrefix(strings.ToLower(proxyURL.Scheme), "https") {
×
125
                        log.FromContext(ctx).
×
126
                                Warnf(`setting "storage.proxy_uri" (%s) is not using https`, rawURL)
×
127
                }
×
128
                options.SetProxyURI(proxyURL)
×
129
        }
130
        return azblob.New(ctx, c.GetString(dconfig.SettingStorageBucket), options)
×
131
}
132

133
func SetupObjectStorage(ctx context.Context) (objManager storage.ObjectStorage, err error) {
1✔
134
        c := config.Config
1✔
135

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

167
func RunServer(ctx context.Context) error {
1✔
168
        c := config.Config
1✔
169
        dbClient, err := mstore.NewMongoClient(ctx, c)
1✔
170
        if err != nil {
1✔
171
                return err
×
172
        }
×
173
        defer func() {
1✔
174
                _ = dbClient.Disconnect(context.Background())
×
175
        }()
×
176

177
        ds := mstore.NewDataStoreMongoWithClient(dbClient)
1✔
178

1✔
179
        // Storage Layer
1✔
180
        objStore, err := SetupObjectStorage(ctx)
1✔
181
        if err != nil {
1✔
182
                return errors.WithMessage(err, "main: failed to setup storage client")
×
183
        }
×
184

185
        app := app.NewDeployments(ds, objStore, 0, false)
1✔
186
        if addr := c.GetString(dconfig.SettingReportingAddr); addr != "" {
2✔
187
                c := reporting.NewClient(addr)
1✔
188
                app = app.WithReporting(c)
1✔
189
        }
1✔
190

191
        // Setup API Router configuration
192
        base64Repl := strings.NewReplacer("-", "+", "_", "/", "=", "")
1✔
193
        expireSec := c.GetDuration(dconfig.SettingPresignExpireSeconds)
1✔
194
        apiConf := api.NewConfig().
1✔
195
                SetPresignExpire(time.Second * expireSec).
1✔
196
                SetPresignHostname(c.GetString(dconfig.SettingPresignHost)).
1✔
197
                SetPresignScheme(c.GetString(dconfig.SettingPresignScheme)).
1✔
198
                SetMaxImageSize(c.GetInt64(dconfig.SettingStorageMaxImageSize)).
1✔
199
                SetEnableDirectUpload(c.GetBool(dconfig.SettingStorageEnableDirectUpload)).
1✔
200
                SetEnableDirectUploadSkipVerify(c.GetBool(dconfig.SettingStorageDirectUploadSkipVerify)).
1✔
201
                SetDisableNewReleasesFeature(c.GetBool(dconfig.SettingDisableNewReleasesFeature))
1✔
202
        if key, err := base64.RawStdEncoding.DecodeString(
1✔
203
                base64Repl.Replace(
1✔
204
                        c.GetString(dconfig.SettingPresignSecret),
1✔
205
                ),
1✔
206
        ); err == nil {
2✔
207
                apiConf.SetPresignSecret(key)
1✔
208
        }
1✔
209
        router, err := api.NewRouter(ctx, app, ds, apiConf)
1✔
210
        if err != nil {
1✔
211
                return err
×
212
        }
×
213

214
        api := rest.NewApi()
1✔
215
        SetupMiddleware(c, api)
1✔
216
        api.SetApp(router)
1✔
217

1✔
218
        listen := c.GetString(dconfig.SettingListen)
1✔
219

1✔
220
        if c.IsSet(dconfig.SettingHttps) {
1✔
221

×
222
                cert := c.GetString(dconfig.SettingHttpsCertificate)
×
223
                key := c.GetString(dconfig.SettingHttpsKey)
×
224

×
225
                return http.ListenAndServeTLS(listen, cert, key, api.MakeHandler())
×
226
        }
×
227

228
        return http.ListenAndServe(listen, api.MakeHandler())
1✔
229
}
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