/[pdpsoft]/nl.nikhef.pdp.tcs/nl.nikhef.pdp.tcs.dctcs-cli/trunk/dctcs-listdomains.cin
ViewVC logotype

Contents of /nl.nikhef.pdp.tcs/nl.nikhef.pdp.tcs.dctcs-cli/trunk/dctcs-listdomains.cin

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3277 - (show annotations) (download)
Sun Jan 26 09:23:38 2020 UTC (2 years, 8 months ago) by davidg
File size: 4818 byte(s)
Added new tool

1 #! /usr/bin/perl -w
2 #
3 # dctcs-listdomains.pl - DigiCert TCS listing of domains and organisations
4 #
5 # @(#)$Id$
6 # David Groep, Nikhef, 2020 - www.nikhef.nl/pdp
7 #
8 # As per doc https://www.digicert.com/services/v2/documentation
9 #
10 use strict;
11 use LWP::UserAgent;
12 use LWP::Protocol::https;
13 use IO::Socket::SSL;
14 use JSON;
15 use Data::Dumper;
16 use Getopt::Long;
17 $Getopt::Long::ignorecase = 0;
18
19 # ###########################################################################
20 # basic configuration - you SHOULD probably change apikeyfile and orgid!
21 # where apikeyfile may be the empty string (will then ask key from STDIN)
22 #
23 my $resturl = "https://www.digicert.com/services/v2";
24 my $apikeylen = 82; # length in characters of API key, seems to be 47 or 82
25 #
26 # CONFIGURE these values or override with args each time
27 my $apikeyfile = "@APIKEYFILE@"; # CONFIG: provide your own filename here
28
29 my $hostname;
30 my $outfile;
31 my $help = 0;
32 my $verb = 0;
33 my $activeonly = 0;
34
35 &GetOptions(
36 'o|outfile=s' => \$outfile,
37 'K|keyfile=s' => \$apikeyfile,
38 'activeonly+' => \$activeonly,
39 'v+' => \$verb,
40 'h|help' => \$help
41 ) or exit 1;
42
43 if ( $help ) { &help; exit 0 }
44
45 # ###########################################################################
46 #
47 # validate options and input
48 # NONE for now
49
50 # ###########################################################################
51 #
52 # read password if needed from file or use env var DIGICERTAPIKEY or STDIN
53 my $apikey;
54 if ( $apikeyfile ne "none" && $apikeyfile ne "" && -e $apikeyfile ) {
55 open FH,"<$apikeyfile";
56 $apikey = <FH>; chomp($apikey);
57 close FH;
58 } elsif (defined($ENV{DIGICERTAPIKEY})) {
59 $apikey = $ENV{DIGICERTAPIKEY};
60 } else {
61 print "Provide API key: ";
62 system("stty -echo");
63 $apikey = <STDIN>; chomp($apikey);
64 system("stty echo"); print "***\n";
65 }
66 #die "Invalid API key length\n" if length($apikey) != $apikeylen;
67
68 # ###########################################################################
69 # setup defaults and LWP
70 #
71 # initialise UA
72 my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
73 $ua->agent("dctcs-listdomains/0.1 (libwww-perl/$]; TERENA-TCS; $^O)");
74 $ua->default_header('X-DC-DEVKEY' => $apikey);
75 $ua->default_header('Content-Type' => "application/json");
76 $ua->default_header('Accept' => "application/json");
77
78 # ###########################################################################
79 # Actions
80 #
81
82 $verb and print "Retrieving list of domains ...\n";
83
84 my $domaindata =
85 &getDump($ua,"GET","domain?include_validation=true");
86
87 die "Division access invalid or no domains registered\n"
88 unless defined $domaindata->{"domains"}[0]{"id"};
89
90 my $of;
91 $outfile = "-" unless defined $outfile and $outfile ne "";
92 open $of,">$outfile" or die "Cannot open $outfile: $!\n";
93
94
95 #print Dumper($domaindata);
96
97 foreach my $dom ( @{$domaindata->{"domains"}} ) {
98 next if $activeonly and ! ($dom->{"is_active"} and $dom->{"organization"}{"is_active"});
99 printf $of "%s,%s\n",$dom->{"name"},$dom->{"organization"}{"name"};
100 }
101
102 close $of;
103
104
105 exit 0;
106
107 # ###########################################################################
108 #
109
110 # getDump($ua,"(GET|PUT|GETDUMP|PUTDUMP|POST|POSTDUMP)",$url,[$content])
111 # where the "DUMP" modes will return plain text from the answer, but
112 # the default modes will return a perl object created from the JSON
113 #
114 sub getDump($$$$) {
115 my ($ua,$type,$request,$content) = @_;
116 my $data;
117 $type = "GET" unless (defined $type and $type ne "");
118 die "Invalid call with GET and contents\n"
119 if ( $type eq "GET" and defined $content and $content ne "");
120
121 my $outtype = $type;
122 $type =~ s/DUMP$//;
123
124 my $req = HTTP::Request->new($type => "$resturl/$request");
125 if ( ( $type eq "POST" || $type eq "PUT" )
126 and defined $content and $content ne "" ) {
127 $req->content($content);
128 }
129
130 my $res = $ua->request($req);
131
132 if ($res->is_success) {
133 if ( $outtype =~ /DUMP/ ) { $data = $res->content; }
134 else { $data = from_json($res->content); }
135 } else {
136 die "Invalid API call: ", $res->status_line, "\n";
137 }
138 return $data;
139 }
140
141 # example of a specific API wrapper
142 sub getContainerId($) {
143 my ($ua) = @_;
144 my $data;
145
146 my $req = HTTP::Request->new(GET => "$resturl/user/me");
147 my $res = $ua->request($req);
148
149 if ($res->is_success) {
150 $data = from_json($res->content);
151 } else {
152 die "Invalid API call: ", $res->status_line, "\n";
153 }
154 return $data->{container}{id};
155 }
156
157
158 # ###########################################################################
159 # HELP
160
161 sub help() {
162
163 ( my $base = $0 ) =~ s/^.*\///;
164 print <<EOF;
165 Retrieve list of domains per division from the TCS DigiCert service via the API
166
167 $base [-o outfile] [-v] [-K keyfile]
168
169 -K keyfile file with the API key for the user as a single line
170 -o outfile file to write output to (default: STDOUT)
171
172 EOF
173 return 0;
174 }

Properties

Name Value
svn:executable *

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28