@@ -2,7 +2,6 @@ package clicapi
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"fmt"
7
6
"io"
8
7
"net/url"
@@ -155,34 +154,36 @@ func (cli *cliCapi) newRegisterCmd() *cobra.Command {
155
154
return cmd
156
155
}
157
156
157
+ type capiStatus struct {
158
+ authenticated bool
159
+ enrolled bool
160
+ subscriptionType string
161
+ }
162
+
158
163
// queryCAPIStatus checks if the Central API is reachable, and if the credentials are correct. It then checks if the instance is enrolled in the console.
159
- func queryCAPIStatus (ctx context.Context , db * database.Client , hub * cwhub.Hub , credURL string , login string , password string ) (bool , bool , error ) {
164
+ func queryCAPIStatus (ctx context.Context , db * database.Client , hub * cwhub.Hub , credURL string , login string , password string ) (capiStatus , error ) {
160
165
apiURL , err := url .Parse (credURL )
161
166
if err != nil {
162
- return false , false , err
167
+ return capiStatus {} , err
163
168
}
164
169
165
170
itemsForAPI := hub .GetInstalledListForAPI ()
166
171
167
- if len (itemsForAPI ) == 0 {
168
- return false , false , errors .New ("no scenarios or appsec-rules installed, abort" )
169
- }
170
-
171
172
passwd := strfmt .Password (password )
172
173
173
174
client , err := apiclient .NewClient (& apiclient.Config {
174
175
MachineID : login ,
175
176
Password : passwd ,
176
177
URL : apiURL ,
177
- // I don't believe papi is neede to check enrollement
178
+ // I don't believe papi is needed to check enrollement
178
179
// PapiURL: papiURL,
179
180
VersionPrefix : "v3" ,
180
181
UpdateScenario : func (_ context.Context ) ([]string , error ) {
181
182
return itemsForAPI , nil
182
183
},
183
184
})
184
185
if err != nil {
185
- return false , false , err
186
+ return capiStatus {} , err
186
187
}
187
188
188
189
pw := strfmt .Password (password )
@@ -195,20 +196,20 @@ func queryCAPIStatus(ctx context.Context, db *database.Client, hub *cwhub.Hub, c
195
196
196
197
authResp , _ , err := client .Auth .AuthenticateWatcher (ctx , t )
197
198
if err != nil {
198
- return false , false , err
199
+ return capiStatus {} , err
199
200
}
200
201
201
202
if err := db .SaveAPICToken (ctx , apiclient .TokenDBField , authResp .Token ); err != nil {
202
- return false , false , err
203
+ return capiStatus {} , err
203
204
}
204
205
205
206
client .GetClient ().Transport .(* apiclient.JWTTransport ).Token = authResp .Token
206
207
207
208
if client .IsEnrolled () {
208
- return true , true , nil
209
+ return capiStatus { true , true , client . GetSubscriptionType ()} , nil
209
210
}
210
211
211
- return true , false , nil
212
+ return capiStatus { true , false , "" } , nil
212
213
}
213
214
214
215
func (cli * cliCapi ) Status (ctx context.Context , db * database.Client , out io.Writer , hub * cwhub.Hub ) error {
@@ -223,17 +224,18 @@ func (cli *cliCapi) Status(ctx context.Context, db *database.Client, out io.Writ
223
224
fmt .Fprintf (out , "Loaded credentials from %s\n " , cfg .API .Server .OnlineClient .CredentialsFilePath )
224
225
fmt .Fprintf (out , "Trying to authenticate with username %s on %s\n " , cred .Login , cred .URL )
225
226
226
- auth , enrolled , err := queryCAPIStatus (ctx , db , hub , cred .URL , cred .Login , cred .Password )
227
+ status , err := queryCAPIStatus (ctx , db , hub , cred .URL , cred .Login , cred .Password )
227
228
if err != nil {
228
229
return fmt .Errorf ("failed to authenticate to Central API (CAPI): %w" , err )
229
230
}
230
231
231
- if auth {
232
+ if status . authenticated {
232
233
fmt .Fprint (out , "You can successfully interact with Central API (CAPI)\n " )
233
234
}
234
235
235
- if enrolled {
236
+ if status . enrolled {
236
237
fmt .Fprint (out , "Your instance is enrolled in the console\n " )
238
+ fmt .Fprintf (out , "Subscription type: %s\n " , status .subscriptionType )
237
239
}
238
240
239
241
switch * cfg .API .Server .OnlineClient .Sharing {
0 commit comments