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

mendersoftware / deployments / 1036074718

13 Oct 2023 02:44PM UTC coverage: 78.132% (-2.8%) from 80.913%
1036074718

Pull #939

gitlab-ci

MuchoLucho
Fix: Make comment more readable for users that may end up troubleshooting
to this point

Changelog: None
Ticket: None

Signed-off-by: Luis Ramirez Vargas <luis.ramirez@northern.tech>
Pull Request #939: Fix: Make comment more readable for users

3 of 3 new or added lines in 1 file covered. (100.0%)

483 existing lines in 6 files now uncovered.

3998 of 5117 relevant lines covered (78.13%)

56.11 hits per line

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

62.26
/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
                SetUseAccelerate(c.GetBool(dconfig.SettingAwsS3UseAccelerate))
1✔
52

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

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

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

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

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

126
func SetupObjectStorage(ctx context.Context) (objManager storage.ObjectStorage, err error) {
1✔
127
        c := config.Config
1✔
128

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

160
func RunServer(ctx context.Context) error {
1✔
161
        c := config.Config
1✔
162
        dbClient, err := mstore.NewMongoClient(ctx, c)
1✔
163
        if err != nil {
1✔
164
                return err
×
165
        }
×
166
        defer func() {
1✔
167
                _ = dbClient.Disconnect(context.Background())
×
168
        }()
×
169

170
        ds := mstore.NewDataStoreMongoWithClient(dbClient)
1✔
171

1✔
172
        // Storage Layer
1✔
173
        objStore, err := SetupObjectStorage(ctx)
1✔
174
        if err != nil {
1✔
175
                return errors.WithMessage(err, "main: failed to setup storage client")
×
176
        }
×
177

178
        app := app.NewDeployments(ds, objStore, 0, false)
1✔
179
        if addr := c.GetString(dconfig.SettingReportingAddr); addr != "" {
2✔
180
                c := reporting.NewClient(addr)
1✔
181
                app = app.WithReporting(c)
1✔
182
        }
1✔
183

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

206
        api := rest.NewApi()
1✔
207
        SetupMiddleware(c, api)
1✔
208
        api.SetApp(router)
1✔
209

1✔
210
        listen := c.GetString(dconfig.SettingListen)
1✔
211

1✔
212
        if c.IsSet(dconfig.SettingHttps) {
1✔
UNCOV
213

×
214
                cert := c.GetString(dconfig.SettingHttpsCertificate)
×
215
                key := c.GetString(dconfig.SettingHttpsKey)
×
216

×
217
                return http.ListenAndServeTLS(listen, cert, key, api.MakeHandler())
×
218
        }
×
219

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