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

mindersec / minder / 18553979900

16 Oct 2025 07:39AM UTC coverage: 57.814% (+0.3%) from 57.553%
18553979900

Pull #5899

github

web-flow
Merge 20bdbbb89 into f6fa9e20a
Pull Request #5899: Add Provider authentication for DataSources

111 of 160 new or added lines in 8 files covered. (69.38%)

1 existing line in 1 file now uncovered.

18837 of 32582 relevant lines covered (57.81%)

37.07 hits per line

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

59.21
/internal/datasources/service/convert.go
1
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
2
// SPDX-License-Identifier: Apache-2.0
3

4
package service
5

6
import (
7
        "cmp"
8
        "encoding/json"
9
        "errors"
10
        "fmt"
11

12
        "google.golang.org/protobuf/encoding/protojson"
13

14
        "github.com/mindersec/minder/internal/db"
15
        minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
16
        v1datasources "github.com/mindersec/minder/pkg/datasources/v1"
17
)
18

19
// DataSourceMetadata is used to serialize additional datasource-level fields
20
type DataSourceMetadata struct {
21
        Type         string `json:"type"`
22
        ProviderAuth bool   `json:"providerAuth"`
23
}
24

25
func dataSourceDBToProtobuf(ds db.DataSource, dsfuncs []db.DataSourcesFunction) (*minderv1.DataSource, error) {
11✔
26
        outds := &minderv1.DataSource{
11✔
27
                Version: minderv1.VersionV1,
11✔
28
                Type:    string(minderv1.DataSourceResource),
11✔
29
                Id:      ds.ID.String(),
11✔
30
                Name:    ds.Name,
11✔
31
                Context: &minderv1.ContextV2{
11✔
32
                        ProjectId: ds.ProjectID.String(),
11✔
33
                },
11✔
34
        }
11✔
35

11✔
36
        if len(dsfuncs) == 0 {
11✔
37
                return nil, errors.New("data source is invalid and has no defintions")
×
38
        }
×
39

40
        var metadata DataSourceMetadata
11✔
41
        if ds.Metadata.Valid {
11✔
NEW
42
                if err := json.Unmarshal(ds.Metadata.RawMessage, &metadata); err != nil {
×
NEW
43
                        return nil, fmt.Errorf("unable to unmarshal metadata: %w", err)
×
NEW
44
                }
×
45
        }
46

47
        // If we didn't record the type in metadata, use the first function to guess.
48
        dsfType := cmp.Or(metadata.Type, dsfuncs[0].Type)
11✔
49
        switch dsfType {
11✔
50
        case v1datasources.DataSourceDriverStruct:
×
NEW
51
                outds.Driver = &minderv1.DataSource_Structured{
×
NEW
52
                        Structured: &minderv1.StructDataSource{},
×
NEW
53
                }
×
UNCOV
54
                return dataSourceStructDBToProtobuf(outds, dsfuncs)
×
55
        case v1datasources.DataSourceDriverRest:
10✔
56
                outds.Driver = &minderv1.DataSource_Rest{
10✔
57
                        Rest: &minderv1.RestDataSource{},
10✔
58
                }
10✔
59
                outds.GetRest().ProviderAuth = metadata.ProviderAuth
10✔
60
                return dataSourceRestDBToProtobuf(outds, dsfuncs)
10✔
61
        default:
1✔
62
                return nil, fmt.Errorf("unknown data source type: %s", dsfType)
1✔
63
        }
64
}
65

66
func dataSourceRestDBToProtobuf(ds *minderv1.DataSource, dsfuncs []db.DataSourcesFunction) (*minderv1.DataSource, error) {
10✔
67
        // At this point we have already validated that we have at least one function.
10✔
68
        ds.GetRest().Def = make(map[string]*minderv1.RestDataSource_Def, len(dsfuncs))
10✔
69

10✔
70
        for _, dsf := range dsfuncs {
20✔
71
                key := dsf.Name
10✔
72
                dsfToParse := &minderv1.RestDataSource_Def{}
10✔
73
                if err := protojson.Unmarshal(dsf.Definition, dsfToParse); err != nil {
10✔
74
                        return nil, fmt.Errorf("failed to unmarshal data source definition for %s: %w", key, err)
×
75
                }
×
76

77
                ds.GetRest().Def[key] = dsfToParse
10✔
78
        }
79

80
        return ds, nil
10✔
81
}
82

83
func dataSourceStructDBToProtobuf(ds *minderv1.DataSource, dsfuncs []db.DataSourcesFunction) (*minderv1.DataSource, error) {
×
NEW
84
        ds.GetStructured().Def = make(map[string]*minderv1.StructDataSource_Def, len(dsfuncs))
×
85

×
86
        for _, dsf := range dsfuncs {
×
87
                key := dsf.Name
×
88
                dsfToParse := &minderv1.StructDataSource_Def{
×
89
                        Path: &minderv1.StructDataSource_Def_Path{},
×
90
                }
×
91
                if err := protojson.Unmarshal(dsf.Definition, dsfToParse); err != nil {
×
92
                        return nil, fmt.Errorf("failed to unmarshal data source definition for %s: %w", key, err)
×
93
                }
×
94

NEW
95
                ds.GetStructured().Def[key] = dsfToParse
×
96
        }
97

98
        return ds, nil
×
99
}
100

101
func metadataForDataSource(ds *minderv1.DataSource) (json.RawMessage, error) {
7✔
102
        metadata := DataSourceMetadata{
7✔
103
                Type: v1datasources.DataSourceDriverStruct,
7✔
104
        }
7✔
105
        switch ds.Driver.(type) {
7✔
106
        case *minderv1.DataSource_Rest:
7✔
107
                metadata.Type = v1datasources.DataSourceDriverRest
7✔
108
                metadata.ProviderAuth = ds.GetRest().GetProviderAuth()
7✔
NEW
109
        case *minderv1.DataSource_Structured:
×
NEW
110
                metadata.Type = v1datasources.DataSourceDriverStruct
×
NEW
111
        default:
×
NEW
112
                return nil, fmt.Errorf("unknown datasource driver %T", ds.Driver)
×
113
        }
114
        metadataBytes, err := json.Marshal(metadata)
7✔
115
        if err != nil {
7✔
NEW
116
                return nil, fmt.Errorf("failed to marshal metadata: %w", err)
×
NEW
117
        }
×
118
        return metadataBytes, nil
7✔
119
}
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