Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mraineri committed Apr 27, 2017
0 parents commit d2aadd8
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 0 deletions.
8 changes: 8 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# Original Contribution:
* Majec Systems


# Other Key Contributions:


5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Change Log

## [0.9.0] - 2017-04-06
- Initial Public Release
57 changes: 57 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
The Distributed Management Task Force (DMTF) grants rights under copyright in
this software on the terms of the BSD 3-Clause License as set forth below; no
other rights are granted by DMTF. This software might be subject to other rights
(such as patent rights) of other parties.


### Copyrights.

Copyright (c) 2016, Contributing Member(s) of Distributed Management Task Force,
Inc.. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of the Distributed Management Task Force (DMTF) nor the names
of its contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


### Patents.

This software may be subject to third party patent rights, including provisional
patent rights ("patent rights"). DMTF makes no representations to users of the
standard as to the existence of such rights, and is not responsible to
recognize, disclose, or identify any or all such third party patent right,
owners or claimants, nor for any incomplete or inaccurate identification or
disclosure of such rights, owners or claimants. DMTF shall have no liability to
any party, in any manner or circumstance, under any legal theory whatsoever, for
failure to recognize, disclose, or identify any such third party patent rights,
or for such party's reliance on the software or incorporation thereof in its
product, protocols or testing procedures. DMTF shall have no liability to any
party using such software, whether such use is foreseeable or not, nor to any
patent owner or claimant, and shall have no liability or responsibility for
costs or losses incurred if software is withdrawn or modified after publication,
and shall be indemnified and held harmless by any party using the software from
any and all claims of infringement by a patent owner for such use.

DMTF Members that contributed to this software source code might have made
patent licensing commitments in connection with their participation in the DMTF.
For details, see http://dmtf.org/sites/default/files/patent-10-18-01.pdf and
http://www.dmtf.org/about/policies/disclosures.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright 2016 Distributed Management Task Force, Inc. All rights reserved.
# Redfish Reference Checker - Version 0.9

## About
RedfishReferenceTool.py is a python3 tool that checks for valid reference URLs in CSDL xml files.

## Pre-requisites
* beautifulsoup4 - https://pypi.python.org/pypi/beautifulsoup4/4.1.3
* requests - https://github.com/kennethreitz/requests (Documentation is available at http://docs.python-requests.org/)

## Installation
Copy RedfishReferenceTool.py into any tool directory, and requires no extra configuration.

Run: python3 RedfishReferenceTool.py [url] [--nocert]

Note that quotations or an escape must be used for '$' and '#' characters, when using ODATA Uris.

## Execution
Upon execution, attempts to get an XML file at the URL given, and exits with 1
on bad URLs or non xml formatted files, then dereferences all reference URLs
in the file.

Exits with success if the amount of missing references is zero.

106 changes: 106 additions & 0 deletions RedfishReferenceTool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Copyright Notice:
# Copyright 2016 Distributed Management Task Force, Inc. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Reference-Checker/LICENSE.md

from bs4 import BeautifulSoup
import os
import sys
import requests


def getSchemaFile(url, chkCert=False):
"""
Get a schemafile from a URL, returns None if it fails.
:param url: given URL
:returns a Soup object
"""

soup = None
filedata = None
success = False
status = ''
# rs-assertion: no authorization expected for schemagrabbing
try:
r = requests.get(url,verify=chkCert)
filedata = r.text
status = r.status_code
doctype = r.headers.get('content-type')
# check if file is returned and is legit XML
if "xml" in doctype and status in [200, 204] and filedata:
soup = BeautifulSoup(filedata, "html.parser")
success = True
except Exception as ex:
sys.stderr.write("Something went wrong: %s %s" % (ex, status))

return success, soup, status


def getRefs(soup):
"""
Get reference URLs from a legitimate schema.
param: soup: bs4 soup object
return: list of refs
"""
references = soup.find_all('edmx:reference')
refurls = [ref.get('uri') for ref in references]
return refurls


if __name__ == "__main__":
rootURL = None
chkCert = True

for arg in sys.argv[1:]:
if arg == '--nocert' and chkCert == True:
chkCert = False
elif rootURL is None:
rootURL = arg
else:
print("invalid option:", arg)
sys.exit(1)
if len(sys.argv) > 3 or rootURL is None:
print("usage: RedfishReferenceTool.py [url] [--nocert]")
sys.exit(1)
rootHost = rootURL.rsplit('/',rootURL.count('/')-2)[0]
print(rootHost)
print(rootURL)
success, rootSoup, status = getSchemaFile(rootURL, chkCert)

if not success:
print("No Schema Found at URL")
sys.exit(1)

missingRefs = 0
refs = getRefs(rootSoup)
allRefs = set()

# loop that finds all URLs and checks for missing or malformed items
while len(refs) > 0:
newRefs = []
for ref in refs:
if ref in allRefs:
continue
allRefs.add(ref)
print(ref)
success, soupOfRef, status = getSchemaFile(ref if 'http' in ref[:8] else rootHost+ref, chkCert = chkCert)
if success:
newRefs += getRefs(soupOfRef)
else:
print (success, soupOfRef, status)
missingRefs += 1
print (
"Something went wrong in script: No Valid Schema Found", missingRefs)
refs = newRefs

if len(allRefs) > 0:
print("Work complete, total failures: ", missingRefs)
print("Total references: ", len(allRefs))
else:
print("No references found.")

if missingRefs > 0:
sys.exit(1)

sys.exit(0)
50 changes: 50 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright Notice:
# Copyright 2016 Distributed Management Task Force, Inc. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Reference-Checker/LICENSE.md

import unittest
import RedfishReferenceTool as rst
from bs4 import BeautifulSoup


class TestDMTF(unittest.TestCase):

def test_get(self):
uri = "http://redfish.dmtf.org/schemas/v1/Resource_v1.xml"
success, soup, status = rst.getSchemaFile(uri)

self.assertEqual(success, True)
self.assertGreater(len(soup.find_all("edmx:reference")), 0)

def test_bad_get(self):
uri = "http://redfish.dmtf.org/index.html"
success, soup, status = rst.getSchemaFile(uri)
self.assertEqual(success, False)

def test_bad_get_2(self):
uri = "http://baduri"
success, soup, status = rst.getSchemaFile(uri)
self.assertEqual(success, False)

def test_find_refs(self):
samplexml = '<edmx:Reference Uri="http://docs.oasis-open.org/odata/odata/v4.0/errata03/csd01/complete/vocabularies/Org.OData.Core.V1.xml">\
<edmx:Include Namespace="Org.OData.Core.V1" Alias="OData"/>\
</edmx:Reference>\
<edmx:Reference Uri="http://redfish.dmtf.org/schemas/v1/RedfishExtensions_v1.xml">\
<edmx:Include Namespace="RedfishExtensions.v1_0_0" Alias="Redfish"/>\
</edmx:Reference>\
<edmx:Reference Uri="http://redfish.dmtf.org/schemas/v1/Resource_v1.xml">\
<edmx:Include Namespace="Resource"/>\
<edmx:Include Namespace="Resource.v1_0_0"/>\
</edmx:Reference>'

allRefs = ["http://docs.oasis-open.org/odata/odata/v4.0/errata03/csd01/complete/vocabularies/Org.OData.Core.V1.xml",
"http://redfish.dmtf.org/schemas/v1/RedfishExtensions_v1.xml", "http://redfish.dmtf.org/schemas/v1/Resource_v1.xml"]

soup = BeautifulSoup(samplexml, "html.parser")

self.assertEqual(allRefs, rst.getRefs(soup))


if __name__ == '__main__':
unittest.main()

0 comments on commit d2aadd8

Please sign in to comment.